#define NDEBUG // disable assertion messages for boost multi_arrays
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
//#include <boost/chrono.hpp>
#include <iostream.h>
//#include <fcntl.h>
#include <io.h>
#include <sstream>
#include <algorithm>    // std::copy
#include <boost/array.hpp>
#include <boost/multi_array.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
//#include <cassert>
#include <iterator>
#define _POSIX_SOURCE
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include <unistd.h>
#undef _POSIX_SOURCE
#include <windows.h>
#include <math.h>
#include <map>
#include <time.h>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace std;

// sprintf(debugmsg,"%u",chartbars[displayedfile]);MessageBox( NULL, debugmsg,"Debug",MB_OK); use this where you need to debug the program
static char debugmsg[1000];

char* writetofile ( char*,int );
long isleapyear(long);
int DateFromSerial(double , long& , long& , long&, long&, long&, double&);
long dateserial (long,long,long);
int ibarshift (int,double,int,bool);
char* idate(int,int);
char* itime(int,int);
double iopen(int,int);
double ihigh(int,int);
double ilow(int,int);
double iclose(int,int);
int ivolume(int,int);
double idatetimeserial(int,int);
double iapplied_price(int,int,int);
double itype(int,int,int);
double ima(int,int,int,int,int,int);
double irsi(int,int,int,int);
double istochastic(int,int,int,int,int,int,int,int);
double icci(int,int,int,int);
double iatr(int,int,int);
double iadx(int,int,int,int,int);
double irvi(int,int,int,int);
int ihighest(int,int,int,int);
int ilowest(int,int,int,int);
double calculate_seconds_since_1_1_1970(long,long,long, int, int, int);
double timeb(int);
double timeminute(double);
double timehour(double);
int timedayofweekold(double);
int timeday(double);
int timemonth(double);
double mathmax(double,double);
double mathmin(double,double);
double itimeb(int,int);
char* ut_to_date(long);
int timedayofweek(double);
void DoEvents();
#define FLOAT double
int toconic(FLOAT p0[3], FLOAT p1[3], FLOAT p2[3], FLOAT p3[3], FLOAT p4[3], FLOAT *a, FLOAT *b, FLOAT *c, FLOAT *d, FLOAT *e, FLOAT *f);
char* unix_time_to_datecpp(double);

typedef boost::array<char*,5000> arrayofchars;
typedef boost::array<double,5000> arrayofdoubles;
typedef boost::array<int,5000> arrayofints;
typedef boost::array<long,5000> arrayoflongs;
boost::multi_array<char*, 2> static grid(boost::extents[281][10000]);
boost::multi_array<char*, 2> static gridtmp(boost::extents[281][10000]);

double imaonarray(arrayofdoubles&,int,int,int,int,int);
double irsionarray(arrayofdoubles&,int,int,int);

static arrayofchars datea;
static arrayofchars date1;
static arrayofchars date5;
static arrayofchars date15;
static arrayofchars date30;
static arrayofchars date60;
static arrayofchars date240;
static arrayofchars date1440;
static arrayofchars date10080;
static arrayofchars date43200;
static arrayofchars timea;
static arrayofchars time1;
static arrayofchars time5;
static arrayofchars time15;
static arrayofchars time30;
static arrayofchars time60;
static arrayofchars time240;
static arrayofchars time1440;
static arrayofchars time10080;
static arrayofchars time43200;
static arrayofdoubles opena;
static arrayofdoubles open1;
static arrayofdoubles open5;
static arrayofdoubles open15;
static arrayofdoubles open30;
static arrayofdoubles open60;
static arrayofdoubles open240;
static arrayofdoubles open1440;
static arrayofdoubles open10080;
static arrayofdoubles open43200;
static arrayofdoubles higha;
static arrayofdoubles high1;
static arrayofdoubles high5;
static arrayofdoubles high15;
static arrayofdoubles high30;
static arrayofdoubles high60;
static arrayofdoubles high240;
static arrayofdoubles high1440;
static arrayofdoubles high10080;
static arrayofdoubles high43200;
static arrayofdoubles lowa;
static arrayofdoubles low1;
static arrayofdoubles low5;
static arrayofdoubles low15;
static arrayofdoubles low30;
static arrayofdoubles low60;
static arrayofdoubles low240;
static arrayofdoubles low1440;
static arrayofdoubles low10080;
static arrayofdoubles low43200;
static arrayofdoubles closea;
static arrayofdoubles close1;
static arrayofdoubles close5;
static arrayofdoubles close15;
static arrayofdoubles close30;
static arrayofdoubles close60;
static arrayofdoubles close240;
static arrayofdoubles close1440;
static arrayofdoubles close10080;
static arrayofdoubles close43200;
static arrayofints volumea; 
static arrayofints volume1;
static arrayofints volume5;
static arrayofints volume15;
static arrayofints volume30;
static arrayofints volume60;
static arrayofints volume240;
static arrayofints volume1440;
static arrayofints volume10080;
static arrayofints volume43200;
static arrayofdoubles datetimeserial; 
static arrayofdoubles datetimeserial1;
static arrayofdoubles datetimeserial5;
static arrayofdoubles datetimeserial15;
static arrayofdoubles datetimeserial30;
static arrayofdoubles datetimeserial60;
static arrayofdoubles datetimeserial240;
static arrayofdoubles datetimeserial1440;
static arrayofdoubles datetimeserial10080;
static arrayofdoubles datetimeserial43200;
static arrayofchars datef;
static arrayofchars timef;
static arrayofdoubles openf;
static arrayofdoubles highf;
static arrayofdoubles lowf;
static arrayofdoubles closef;
static arrayofints volumef;
static arrayofints chartbars;
static arrayofints chartbarstmp;
static arrayofints charttf;
static arrayofchars chartdatasource;
static arrayofchars chartsymbolname;
static int displayedfile;
static int numbars_first;
static int numbars_last;
static int numbars;
static int rowgridoffset=0;
static int openedfilesnb=0;
static int bars;  
static arrayofchars importedfile; 
static int cntbarsedit=2000;  
const int PERIOD_M1=1;
const int PERIOD_M5=5;
const int PERIOD_M15=15;
const int PERIOD_M30=30;
const int PERIOD_H1=60;
const int PERIOD_H4=240;
const int PERIOD_D1=1440;
const int PERIOD_W1=10080;
const int PERIOD_MN1=43200;
const int MODE_SMA=0;
const int MODE_EMA=1;
const int MODE_SMMA=2;
const int MODE_LWMA=3;
const int PRICE_CLOSE=0;
const int PRICE_OPEN=1;
const int PRICE_HIGH=2;
const int PRICE_LOW=3;
const int PRICE_MEDIAN=4;
const int PRICE_TYPICAL=5;
const int PRICE_WEIGHTED=6;
const int MODE_OPEN=0;
const int MODE_LOW=1;
const int MODE_HIGH=2;
const int MODE_CLOSE=3;
const int MODE_VOLUME=4;
const int MODE_TIME=5;
// imacd, irvi, istochastic
const int MODE_MAIN=0;
const int MODE_SIGNAL=1;
// iadx
const int MODE_PLUSDI=1;
const int MODE_MINUSDI=2;
// ibands, ienvelopes, ienvelopesonarray, ifractals
const int MODE_UPPER=1;
const int MODE_LOWER=2;
// ialligator, igator
const int MODE_GATORJAW=1;
const int MODE_GATORTEETH=2;
const int MODE_GATORLIPS=3;
// iichimoku
const int MODE_TENKANSEN=1;
const int MODE_KIJUNSEN=2;
const int MODE_SENKOUSPANA=3;
const int MODE_SENKOUSPANB=4;
const int MODE_CHIKOUSPAN=5;
const bool True=1;
const bool False=0;

std::string replaceSubstring(const std::string& string1,const std::string& string2,const std::string& string3)
{
    std::string newString;
    for (size_t i=0;i<string1.length();i++)
    {
        if (string1.substr(i,string2.length())==string2)
        {
            newString+=string3;
            i+=string2.length()-1; //skip other matched characters
        }
        else newString+=string1[i];
    }
    return newString;
}

inline int mypow(int a, int b)
{
    if (b==0) return 1;
    if (a==0) return 0;
    return a*mypow(a, b-1);
}

char* filegetline ( int linenb, char* flnme )
{
   static char filename[1000];
   strncpy(filename, flnme, 1000);
   FILE *file;
	int fd;
	fd = open(filename,O_RDONLY);
if(fd<0){
    printf("open call fail");
    //return -1;
 }
   file = fdopen ( fd, "r" );
//file = fopen ( filename, "r" );
	//printf("%d\n", fileno(file));
   int lineinc=0;        
   if ( file != NULL )
   {      
        
      char line [ 128 ]; /* or other suitable maximum line size */
      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
        lineinc++;
        if (lineinc==linenb)
        {  
            fclose ( file );
            close (fd);
            return line;
            break;
        }
      }
      	
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
	strerror(errno);
   }
   //return 0; 
   
}

char* filegetalllines ( char* flnme,char* reinit )
{    
    static long int i;
    static char reinitt[1000];
   strncpy(reinitt,reinit,1000);
   if (atoi(reinitt)==1) 
    { 
    i=0;
    return ""; 
    }
   static char filename[1000];
   strncpy(filename, flnme, 1000);      
   
   FILE *file;
	int fd;
	fd = open(filename,O_RDONLY);
if(fd<0){
    printf("open call fail");
    //return -1;
 }
   file = fdopen ( fd, "r" );
//file = fopen ( filename, "r" );
	//printf("%d\n", fileno(file));
   int lineinc=0;        
   if ( file != NULL )
   {      	       

      fseek ( file ,i  , SEEK_SET );  
      char line [ 128 ]; /* or other suitable maximum line size */
      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
        lineinc++;
        if (lineinc==1)
        {  
            
            i=ftell(file);
            fclose ( file );
            close (fd);          
            return line;
            break;
        }
      }
      	
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
	strerror(errno);
   }
   //return 0; 
   
}

int filegetlinecount(char* flnme )
{
   static char filename[1000];
   strncpy(filename, flnme, 1000);
   FILE *file = fopen ( filename, "r" );
   int lineinc=0;
   if ( file != NULL )
   {
      char line [ 128 ]; /* or other suitable maximum line size */
      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
         //fputs ( line, stdout ); /* write the line */
	std::string linestr (line);  
        if (linestr.size()>1) lineinc++;
      }
      fclose ( file );
      return lineinc;
      
   }
   else
   {
      //perror ( filename ); /* why didn't the file open? */
	strerror(errno);
   }
   //return 0; 
}

char* ut_to_date(long unix_time)
{

	time_t t = unix_time; // convert to time_t, ignores msec
	char asctimechar[1000];
	sprintf(asctimechar,"%s",asctime(gmtime(&t)));
	return asctimechar;
}

char* convert(const std::string & s)
{
   char *pc = new char[s.size()+1];
   std::strcpy(pc, s.c_str());
   return pc; 
}

char* filegetlinesarray ( char* flnme )
{  
       
   static char filename[1000];
   strncpy(filename, flnme, 1000);      
   FILE *file;
   int fd;
   fd = open(filename,O_RDONLY);

   if(fd<0) {
    		printf("open call fail");
 	    }

   file = fdopen ( fd, "r" );

          
   if ( file != NULL )
   {      
      int i=0;  
      char line [ 128 ]; /* or other suitable maximum line size */       
      //char * writable;    

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
        
        std::string linestr (line); 
	
        if (linestr.size()>1) { 
     	
    	/* Does not work for the volume
        std::size_t found = linestr.find(",");        
        std::string datestr = linestr.substr (0,found);
        writable = new char[datestr.size() + 1];
        std::copy(datestr.begin(), datestr.end(), writable);
        writable[datestr.size()] = '\0'; // don't forget the terminating 0           
        //datef[i]=writable;
        grid[rowgridoffset+1][i+1]=writable;	
        
        linestr.assign(linestr.begin()+found+1,linestr.end());
        found = linestr.find(",");
        std::string timestr = linestr.substr (0,found);
        writable = new char[timestr.size() + 1];
        std::copy(timestr.begin(), timestr.end(), writable);
        writable[timestr.size()] = '\0'; // don't forget the terminating 0
        //timef[i]=writable;
        grid[rowgridoffset+2][i+1]=writable;
        
        linestr.assign(linestr.begin()+found+1,linestr.end());
        found = linestr.find(",");
        std::string openstr = linestr.substr (0,found);
        writable = new char[openstr.size() + 1];
        std::copy(openstr.begin(), openstr.end(), writable);
        writable[openstr.size()] = '\0'; // don't forget the terminating 0
        //openf[i]=atof(writable);
        grid[rowgridoffset+3][i+1]=writable;
        
        linestr.assign(linestr.begin()+found+1,linestr.end());
        found = linestr.find(",");
        std::string highstr = linestr.substr (0,found);
        writable = new char[highstr.size() + 1];
        std::copy(highstr.begin(), highstr.end(), writable);
        writable[highstr.size()] = '\0'; // don't forget the terminating 0
        //highf[i]=atof(writable);
        grid[rowgridoffset+4][i+1]=writable;
        
        linestr.assign(linestr.begin()+found+1,linestr.end());
        found = linestr.find(",");
        std::string lowstr = linestr.substr (0,found);
        writable = new char[lowstr.size() + 1];
        std::copy(lowstr.begin(), lowstr.end(), writable);
        writable[lowstr.size()] = '\0'; // don't forget the terminating 0
        //lowf[i]=atof(writable); 
        grid[rowgridoffset+5][i+1]=writable;
        
        linestr.assign(linestr.begin()+found+1,linestr.end());
        found = linestr.find(",");
        std::string closestr = linestr.substr (0,found);
        writable = new char[closestr.size() + 1];
        std::copy(closestr.begin(), closestr.end(), writable);
        writable[closestr.size()] = '\0'; // don't forget the terminating 0
        //closef[i]=atof(writable);
        grid[rowgridoffset+6][i+1]=writable;
	        
	//linestr.assign(linestr.begin()+found+1,linestr.end());
	found = linestr.rfind(",");
        std::string volumestr = linestr.substr (found);
        writable = new char[volumestr.size() + 1];
        std::copy(volumestr.begin(), volumestr.end(), writable);
        writable[volumestr.size()] = '\0'; // don't forget the terminating 0
        volumef[i]=atoi(writable);  
        grid[rowgridoffset+7][i+1]=writable;

	Does not work for the volume */

// This is the solution to split lines

	vector<string> vs;

	boost::split(vs,linestr,boost::is_any_of(","));
   
	std::vector<char*>  vc;

	std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);   
 
	grid[rowgridoffset+1][i+1]=vc[0];
	grid[rowgridoffset+2][i+1]=vc[1];
	grid[rowgridoffset+3][i+1]=vc[2];
	grid[rowgridoffset+4][i+1]=vc[3];
	grid[rowgridoffset+5][i+1]=vc[4];
	grid[rowgridoffset+6][i+1]=vc[5];
	grid[rowgridoffset+7][i+1]=vc[6];

// End of solution to split lines
 	        
        gridtmp[rowgridoffset+1][i+1]=grid[rowgridoffset+1][i+1];  
        gridtmp[rowgridoffset+2][i+1]=grid[rowgridoffset+2][i+1];  
        gridtmp[rowgridoffset+3][i+1]=grid[rowgridoffset+3][i+1];  
        gridtmp[rowgridoffset+4][i+1]=grid[rowgridoffset+4][i+1];  
        gridtmp[rowgridoffset+5][i+1]=grid[rowgridoffset+5][i+1];  
        gridtmp[rowgridoffset+6][i+1]=grid[rowgridoffset+6][i+1];  
        gridtmp[rowgridoffset+7][i+1]=grid[rowgridoffset+7][i+1];  
    
        //cout << datef[i] << " " << timef[i] << " " << openf[i] << " " << highf[i] << " " << lowf[i] << " " << closef[i] << " " << volumef[i] << "\n";
        //delete[] writable;
       i++;  
  
                         }
      }
      
   fclose ( file );
   close (fd); 
      	
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
	strerror(errno);
   }

   return 0; 
   
}

char* useindifunc (char* cntbarsedit)
{ 
static char cntbarseditt[1000];
strncpy(cntbarseditt, cntbarsedit, 1000);
int o=0;
                        
long year;long month;long day;int hour;int minute;int second;
int ii=chartbars[displayedfile] - atoi(cntbarseditt);
if (ii<1) ii=1;


                        for (int i = chartbars[displayedfile];i>= ii;i--)
                        {
                            datea[o] = grid[rowgridoffset + 1][i];
                            timea[o] = grid[rowgridoffset +  2][i];
                            opena[o] = atof(grid[rowgridoffset +  3][i]);
                            higha[o] = atof(grid[rowgridoffset +  4][i]);
                            lowa[o] = atof(grid[rowgridoffset +  5][i]);
                            closea[o] = atof(grid[rowgridoffset +  6][i]);
                            volumea[o] = atoi(grid[rowgridoffset +  7][i]);
                            std::string dateastr (datea[o]);
                            std::string timeastr (timea[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);                            
                            year=atol(yearstr.c_str());                            
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;

                            //year = VAL(MID$(date(o) , 0 , 4))
                            //month = VAL(MID$(date(o) , 6 , 2))
                            //day = VAL(MID$(date(o) , 9 , 2))
                            //hour = VAL(MID$(time(o) , 0 , 2))
                            //minute = VAL(MID$(time(o) , 4 , 2))
                            datetimeserial[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial[o]=datetimeserial[o]*24*60*60;
                            //datetimeserial[o]=datetimeserial[o]+(hour*60*60);
                            //datetimeserial[o]=datetimeserial[o]+(minute*60);                           
                            o++;
				DoEvents();
                        }
return 0;                        
}

char* writetofile ( char* flnme,int arrsize )
{  

static char filename[1000];
   strncpy(filename, flnme, 1000);
   FILE *file;
	int fd;
	if ((fd = creat(filename, S_IWUSR)) < 0)
    perror("creat() error");  
  else 
  {
  file = fdopen ( fd, "a" );
//file = fopen ( filename, "r" );
	//printf("%d\n", fileno(file));
          
   if ( file != NULL )
   {      
    int i;
    char catline[1000];
    for (i=0;i<=arrsize;i++)
      {
       fputs(datea[i], file);
       fputs(",", file);
       fputs(timea[i], file);
       fputs(",", file);       
       sprintf(catline,"%f",opena[i]);
       fputs(catline, file);
       fputs(",", file);       
       sprintf(catline,"%f",higha[i]);
       fputs(catline, file);
       fputs(",", file);       
       sprintf(catline,"%f",lowa[i]);
       fputs(catline, file);
       fputs(",", file);       
       sprintf(catline,"%f",closea[i]);
       fputs(catline, file);
       fputs(",", file);       
       sprintf(catline,"%u",volumea[i]);
       fputs(catline, file);
       fputs("\n", file);
      }    
      
            
   fclose ( file ); 
   close (fd);
       	
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
	strerror(errno);
   }
   //return 0;
   
   } 
return 0;
}

int delfile(char* flnme )
{
   static char filename[1000];
   strncpy(filename, flnme, 1000);
   if( remove( filename ) != 0 ) perror( "Error deleting file" );
  //else
    //puts( "File successfully deleted" );
  return 0;
}

char* displayedfileminusone ()
{  
displayedfile--;
return 0;
}

char* openedfilesnbminusone ()
{  
openedfilesnb--;
return 0;
}

char* openedfilesnbplusone ()
{  
openedfilesnb++;
return 0;
}

char* displayedfileplusone ()
{  
displayedfile++;
return 0;
}

char* setdisplayedfile (char* displayedfileint)
{  
static char displayedfileintt[1000];
strncpy(displayedfileintt, displayedfileint, 1000);
displayedfile=atoi(displayedfileintt);
return 0;
}

char* setchartbars (char* chartbarsdisplayedfileint,char* chartbarsoffset)
{  
static char chartbarsdisplayedfileintt[1000];
strncpy(chartbarsdisplayedfileintt, chartbarsdisplayedfileint, 1000);
static char chartbarsoffsett[1000];
strncpy(chartbarsoffsett, chartbarsoffset, 1000);
chartbars[atoi(chartbarsoffsett)]=atoi(chartbarsdisplayedfileintt);
return 0;
}

char* setnumbars (char* numbarsint)
{  
static char numbarsintt[1000];
strncpy(numbarsintt, numbarsint, 1000);
numbars=atoi(numbarsintt);
return 0;
}

char* setnumbars_first (char* numbars_firstint)
{  
static char numbars_firstintt[1000];
strncpy(numbars_firstintt, numbars_firstint, 1000);
numbars_first=atoi(numbars_firstintt);
return 0;
}

char* setnumbars_last (char* numbars_lastint)
{  
static char numbars_lastintt[1000];
strncpy(numbars_lastintt, numbars_lastint, 1000);
numbars_last=atoi(numbars_lastintt);
return 0;
}

char* setchartbarstmp (char* chartbarstmpdisplayedfileint,char* chartbarstmpoffset)
{  
static char chartbarstmpdisplayedfileintt[1000];
strncpy(chartbarstmpdisplayedfileintt, chartbarstmpdisplayedfileint, 1000);
static char chartbarstmpoffsett[1000];
strncpy(chartbarstmpoffsett, chartbarstmpoffset, 1000);
chartbarstmp[atoi(chartbarstmpoffsett)]=atoi(chartbarstmpdisplayedfileintt);
return 0;
}

char* setcharttf (char* charttfdisplayedfileint,char* charttfoffset)
{  
static char charttfdisplayedfileintt[1000];
strncpy(charttfdisplayedfileintt, charttfdisplayedfileint, 1000);
static char charttfoffsett[1000];
strncpy(charttfoffsett, charttfoffset, 1000);
charttf[atoi(charttfoffsett)]=atoi(charttfdisplayedfileintt);
return 0;
}

char* setchartdatasource (char* chartdatasourcedisplayedfilechar,char* chartdatasourceoffset)
{  
static char chartdatasourcedisplayedfilecharr[1000];
strncpy(chartdatasourcedisplayedfilecharr, chartdatasourcedisplayedfilechar, 1000);
static char chartdatasourceoffsett[1000];
strncpy(chartdatasourceoffsett, chartdatasourceoffset, 1000);
chartdatasource[atoi(chartdatasourceoffsett)]=chartdatasourcedisplayedfilecharr;
return 0;
}

char* setchartsymbolname (char* chartsymbolnamedisplayedfilechar,char* chartsymbolnameoffset)
{  
static char chartsymbolnamedisplayedfilecharr[1000];
strncpy(chartsymbolnamedisplayedfilecharr, chartsymbolnamedisplayedfilechar, 1000);
static char chartsymbolnameoffsett[1000];
strncpy(chartsymbolnameoffsett, chartsymbolnameoffset, 1000);
chartsymbolname[atoi(chartsymbolnameoffsett)]=chartsymbolnamedisplayedfilecharr;
return 0;
}

char* setrowgridoffset (char* rowgridoffsetint)
{  
static char rowgridoffsetintt[1000];
strncpy(rowgridoffsetintt, rowgridoffsetint, 1000);
rowgridoffset=atoi(rowgridoffsetintt);
return 0;
}

char* setbars (char* barsint)
{  
static char barsintt[1000];
strncpy(barsintt, barsint, 1000);
bars=atoi(barsintt);
return 0;
}

char* setcntbarsedit (char* barsint)
{  
static char barsintt[1000];
strncpy(barsintt, barsint, 1000);
cntbarsedit=atoi(barsintt);
return 0;
}

char* setimportedfile (char* importedfilechar,char* importedfileoffset)
{  
static char importedfilecharr[1000];
strncpy(importedfilecharr, importedfilechar, 1000);
static char importedfileoffsett[1000];
strncpy(importedfileoffsett, importedfileoffset, 1000);
importedfile[atoi(importedfileoffsett)]=importedfilecharr;
return 0;
}

char* shiftgridsonebackward ()
{  
int i;int j;
        for (i = displayedfile;i<=openedfilesnb;i++) {
            for (j = 1; j<= chartbars[i+1];j++) {
                grid[7 * (i - 1) + 1 ][ j] = grid[7 * (i) + 1][j];
                grid[7 * (i - 1) + 2][j] = grid[7 * (i) + 2][j];
                grid[7 * (i - 1) + 3][j] = grid[7 * (i) + 3][j];
                grid[7 * (i - 1) + 4][j] = grid[7 * (i) + 4][j];
                grid[7 * (i - 1) + 5][j] = grid[7 * (i) + 5][j];
                grid[7 * (i - 1) + 6][j] = grid[7 * (i) + 6][j];
                grid[7 * (i - 1) + 7][j] = grid[7 * (i) + 7][j];
                gridtmp[7 * (i - 1) + 1 ][ j] = grid[7 * (i) + 1][j];
                gridtmp[7 * (i - 1) + 2][j] = grid[7 * (i) + 2][j];
                gridtmp[7 * (i - 1) + 3][j] = grid[7 * (i) + 3][j];
                gridtmp[7 * (i - 1) + 4][j] = grid[7 * (i) + 4][j];
                gridtmp[7 * (i - 1) + 5][j] = grid[7 * (i) + 5][j];
                gridtmp[7 * (i - 1) + 6][j] = grid[7 * (i) + 6][j];
                gridtmp[7 * (i - 1) + 7][j] = grid[7 * (i) + 7][j];
                //mixgrid.Cell(j - 1 , i) = mixgrid.Cell(j - 1 , i + 1)
                //mixgrid2.Cell(j - 1 , i) = mixgrid2.Cell(j - 1 , i + 1)
                //mixgrid3.Cell(j - 1 , i) = mixgrid3.Cell(j - 1 , i + 1)
                //mixgrid4.Cell(j - 1 , i) = mixgrid4.Cell(j - 1 , i + 1)
                                      }
            importedfile[i] = importedfile[i + 1];
            chartbars[i] = chartbars[i + 1];
		chartdatasource[i] = chartdatasource[i + 1];
		chartsymbolname[i] = chartsymbolname[i + 1];
		charttf[i] = charttf[i + 1];
            //mixgridcolcount(i) = mixgridcolcount(i + 1)
            //mixgridcolcount2(i) = mixgridcolcount2(i + 1)
            //mixgridcolcount3(i) = mixgridcolcount3(i + 1)
            //mixgridcolcount4(i) = mixgridcolcount4(i + 1)
                                                     }                                      
return 0;
}

char* reversebarscomputesubbcpp (char* graphbarnboncurstaticchar)
{ 
    static char referencialkk[1000];
    strncpy(referencialkk, graphbarnboncurstaticchar, 1000);
    int refk=atoi(referencialkk);
    int i;int j;


    //numbars = VAL(barsdisplayed.Text)

    int k;int l;int m;int incr;
    m = 1;
    incr = 0;

    double kbarlow;double kbarlowprev;
    double kbarhigh;double kbarhighprev;
    double kbaropen;double kbaropenprev;
    double kbarclose;double kbarcloseprev;
    double distcloseopen;double distclosehigh;double distcloselow;
    double distopenclose;double distopenhigh;double distopenlow;

    //char catline[1000];

    for (k = refk;k<=chartbars[displayedfile];k++) {


        kbarlow = atof(gridtmp[rowgridoffset + 5][k]);
        kbarhigh = atof(gridtmp[rowgridoffset + 4][k]);
        kbaropen = atof(gridtmp[rowgridoffset + 3][k]);
        kbarlowprev = atof(gridtmp[rowgridoffset + 5][refk - 1]);
        kbarhighprev = atof(gridtmp[rowgridoffset + 4][refk - 1]);
        kbaropenprev = atof(gridtmp[rowgridoffset + 3][refk - 1]);
        kbarclose = atof(gridtmp[rowgridoffset + 6][k]);
        kbarcloseprev = atof(gridtmp[rowgridoffset + 6][refk - 1]);

        if (kbaropen >= kbarclose) distopenclose = kbaropen - kbarclose;

        if (kbaropen < kbarclose) distopenclose = - 1 * (kbarclose - kbaropen);


        if (kbarhigh >= kbaropen) distopenhigh = - 1 * (kbarhigh - kbaropen);

        if (kbarhigh < kbaropen) distopenhigh = kbaropen - kbarhigh;


        if (kbarlow > kbaropen) distopenlow = - 1 * (kbarlow - kbaropen);

        if (kbarlow <= kbaropen) distopenlow = kbaropen - kbarlow;                               

        if (kbaropen > kbarcloseprev) sprintf(gridtmp[rowgridoffset + 3][k],"%f",kbarcloseprev - (kbaropen - kbarcloseprev));
            //gridtmp[rowgridoffset + 3][k] = catline;
                                      

        if (kbaropen < kbarcloseprev) sprintf(gridtmp[rowgridoffset + 3][k],"%f",kbarcloseprev + (kbarcloseprev - kbaropen));
            //gridtmp[rowgridoffset + 3][k] = catline;
                                      


        //gridtmp[rowgridoffset + 6][k] = ftoa(atof(gridtmp[rowgridoffset + 3][k]) + distopenclose);
        //gridtmp[rowgridoffset + 4][k] = ftoa(atof(gridtmp[rowgridoffset + 3][k]) + distopenlow);
        //gridtmp[rowgridoffset + 5][k] = ftoa(atof(gridtmp[rowgridoffset + 3][k]) + distopenhigh);
        
        sprintf(gridtmp[rowgridoffset + 6][k],"%f",atof(gridtmp[rowgridoffset + 3][k]) + distopenclose);
        sprintf(gridtmp[rowgridoffset + 4][k],"%f",atof(gridtmp[rowgridoffset + 3][k]) + distopenlow);
        sprintf(gridtmp[rowgridoffset + 5][k],"%f",atof(gridtmp[rowgridoffset + 3][k]) + distopenhigh);

        //writealive
                                                           }


    chartbarstmp[displayedfile] = chartbars[displayedfile];
    //chartbarstmpdisplayedfilestr=str$(chartbarstmp(displayedfile)):displayedfilestr=str$(displayedfile):cpptmpfuncreturn=varptr$(setchartbarstmp(varptr(chartbarstmpdisplayedfilestr),varptr(displayedfilestr)))

return 0;
}

char* refreshgridscpp()
{ 
    if (openedfilesnb == 0) return 0;

    int i;
    chartbars[displayedfile] = chartbarstmp[displayedfile];
    //chartbarsdisplayedfilestr=str$(chartbars(displayedfile)):displayedfilestr=str$(displayedfile):cpptmpfuncreturn=varptr$(setchartbars(varptr(chartbarsdisplayedfilestr),varptr(displayedfilestr)))

    for (i = 1;i<=chartbars[displayedfile];i++) {
        grid[rowgridoffset + 1][i] = gridtmp[rowgridoffset + 1][i];
        grid[rowgridoffset + 2][i] = gridtmp[rowgridoffset + 2][i];
        grid[rowgridoffset + 3][i] = gridtmp[rowgridoffset + 3][i];
        grid[rowgridoffset + 4][i] = gridtmp[rowgridoffset + 4][i];
        grid[rowgridoffset + 5][i] = gridtmp[rowgridoffset + 5][i];
        grid[rowgridoffset + 6][i] = gridtmp[rowgridoffset + 6][i];
        grid[rowgridoffset + 7][i] = gridtmp[rowgridoffset + 7][i];
        //writealive
                                                }
    //justrefreshchart
    
    return 0;
}

char* getdatagrid (char* rowgridoffset,char* offset)
{  
static char rowgridoffsett[1000];
strncpy(rowgridoffsett, rowgridoffset, 1000);
static char offsett[1000];
strncpy(offsett, offset, 1000);
return grid[atoi(rowgridoffsett)][atoi(offset)];
}

char* setdatagrid (char* rowgridoffset,char* offset,char* dataval)
{  
static char rowgridoffsett[1000];
strncpy(rowgridoffsett, rowgridoffset, 1000);
static char offsett[1000];
strncpy(offsett, offset, 1000);
static char datavall[1000];
strncpy(datavall, dataval, 1000);
grid[atoi(rowgridoffsett)][atoi(offset)]=datavall;
}

char* getchartbars (char* chartnb)
{  
static char chartnbb[1000];
strncpy(chartnbb, chartnb, 1000);
int chartnbint=atoi(chartnbb);
char catline[1000];
sprintf(catline,"%u",chartbars[chartnbint]);
return catline;
}

char* getchartbarstmp (char* chartnbtmp)
{  
static char chartnbb[1000];
strncpy(chartnbb, chartnbtmp, 1000);
int chartnbint=atoi(chartnbb);
char catline[1000];
sprintf(catline,"%u",chartbarstmp[chartnbint]);
return catline;
}

char* tfmultok_clickcpp(char* tfmult)
{
    static char tfmultt[1000];
strncpy(tfmultt, tfmult, 1000);
int tfmultint=atoi(tfmultt);
    if (openedfilesnb==0) return 0;

    int i;
    int j;
    double high;
    double low;
    int vol;
    int lines;
    lines = 1;    
    chartbars[displayedfile] = chartbarstmp[displayedfile];

    for (i = 1;i<=chartbars[displayedfile];i++) {
        grid[rowgridoffset + 1][i]=gridtmp[rowgridoffset + 1][i];
        grid[rowgridoffset + 2][i]=gridtmp[rowgridoffset + 2][i];
        grid[rowgridoffset + 3][i]=gridtmp[rowgridoffset + 3][i];
        grid[rowgridoffset + 4][i]=gridtmp[rowgridoffset + 4][i];
        grid[rowgridoffset + 5][i]=gridtmp[rowgridoffset + 5][i];
        grid[rowgridoffset + 6][i]=gridtmp[rowgridoffset + 6][i];
        grid[rowgridoffset + 7][i]=gridtmp[rowgridoffset + 7][i];
                                                } 
    for (j = 1;j<=chartbars[displayedfile];j=j+tfmultint) {
        if (j <= (chartbars[displayedfile] - tfmultint + 1)) {
            high = atof(grid[rowgridoffset + 4][j]);
            low = atof(grid[rowgridoffset + 5][j]);
            vol = 0;
            for (i = 1;i<=tfmultint;i++) {
                if (atof(grid[rowgridoffset + 4][j+i-1]) > high) high = atof(grid[rowgridoffset + 4][j+i-1]);
                if (atof(grid[rowgridoffset + 5][j+i-1]) < low) low = atof(grid[rowgridoffset + 5][j+i-1]);
                vol = vol + atoi(grid[rowgridoffset + 7][j+i-1]);
                                         }
            sprintf(grid[rowgridoffset + 4][lines],"%f",high);
            sprintf(grid[rowgridoffset + 5][lines],"%f",low);
            grid[rowgridoffset + 3][lines] = grid[rowgridoffset + 3][j];
            grid[rowgridoffset + 6][lines] = grid[rowgridoffset + 6][j + tfmultint - 1];
            sprintf(grid[rowgridoffset + 7][lines],"%u",vol);
            lines ++;
                                                             }
        if (j > (chartbars[displayedfile] - tfmultint + 1)) {
            high = atof(grid[rowgridoffset + 4][j]);
            low = atof(grid[rowgridoffset + 5][j]);
            vol = 0;
            for (i = 1 ;i<= chartbars[displayedfile] - j + 1;i++) {
                if (atof(grid[rowgridoffset + 4][j+i-1]) > high) high = atof(grid[rowgridoffset + 4][j+i-1]);
                if (atof(grid[rowgridoffset + 5][j+i-1]) < low) low = atof(grid[rowgridoffset + 5][j+i-1]);
                vol = vol + atoi(grid[rowgridoffset + 7][j+i-1]);
                                                                  }
            sprintf(grid[rowgridoffset + 4][lines],"%f",high);
            sprintf(grid[rowgridoffset + 5][lines],"%f",low);
            grid[rowgridoffset + 3][lines] = grid[rowgridoffset + 3][j];
            grid[rowgridoffset + 6][lines] = grid[rowgridoffset + 6][chartbars[displayedfile]];
            sprintf(grid[rowgridoffset + 7][lines],"%u",vol);
            lines ++;
                                                          }
                                                          }
    chartbars[displayedfile] = lines - 1;
    
return 0;    
}

char* savegridtmpcpp()
{

    for (int i = 1;i<=chartbars[displayedfile];i++) {
        gridtmp[rowgridoffset + 1][i] = grid[rowgridoffset + 1][i];
        gridtmp[rowgridoffset + 2][i] = grid[rowgridoffset + 2][i];
        gridtmp[rowgridoffset + 3][i] = grid[rowgridoffset + 3][i];
        gridtmp[rowgridoffset + 4][i] = grid[rowgridoffset + 4][i];
        gridtmp[rowgridoffset + 5][i] = grid[rowgridoffset + 5][i];
        gridtmp[rowgridoffset + 6][i] = grid[rowgridoffset + 6][i];
        gridtmp[rowgridoffset + 7][i] = grid[rowgridoffset + 7][i];
                                                    }
return 0;                                                    
}

long isleapyear(long year)
{
    if ( ( (year % 4) == 0) && ( (year % 100) > 0) || ( (year % 400) == 0) ) return 1;
    else return 0;
}

int ibarshift (int timeframe,double datetimeserial,int limit,bool exact=0)
{
int i;
arrayofdoubles proxiarray;
double proxihighest;double proxilowest;

switch (exact) {
    case 0:
    /* // this thing is false
    proxihighest = 0;
    for (i = 0;i<=limit;i++) {
        proxiarray[i]=static_cast<double>(idatetimeserial(timeframe,i))/static_cast<double>(datetimeserial);
        if (proxiarray[i]>=1) proxiarray[i]=proxiarray[i]-1;
        else proxiarray[i]=1-proxiarray[i];        
        if (proxiarray[i] > proxihighest) proxihighest = proxiarray[i];           
    }
    proxilowest = proxihighest;
    for (i = 0;i<=limit;i++) {
            if (proxiarray[i] < proxilowest) proxilowest = proxiarray[i];            
        }
    for (i = 0;i<=limit;i++) {
            if (proxiarray[i]==proxilowest) {
                return i;
                break;
                                            }
        }   
    */
    for (i = 0;i<=limit;i++) {
	if (i==0 && datetimeserial>=idatetimeserial(timeframe,i)) {
	return i;
	break;
	}
    	if (i>0 && datetimeserial>=idatetimeserial(timeframe,i) && datetimeserial<idatetimeserial(timeframe,i-1)) {
	return i;
        break;
	}
    }
        break;
        
    case 1:    
    for (i = 0;i<=limit;i++) {
	    double nextdatetimeserial=idatetimeserial(timeframe,i)+timeframe*60;
            if ((datetimeserial>=idatetimeserial(timeframe,i)) && (datetimeserial<nextdatetimeserial)) {	    
                return i;
                break;
                                            }
        }
        return -1;   
        break;
  
             }    
return 0;    
}

char* writetfcpp (char* tftowrite)
{
    
 if (openedfilesnb == 0) return 0;

 static char tftowritee[1000];
 strncpy(tftowritee, tftowrite, 1000);
 int tftowriteint=atoi(tftowritee);
 int o;int i;
 char * writable; 
 long year;long month;long day;int hour;int minute; int second;
 int ii=chartbars[displayedfile] - cntbarsedit; 
 if (ii<1) ii=1;

 switch (tftowriteint)
 {
     
     case 1:


                        
                        o = 0;                        

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date1[o] = grid[rowgridoffset + 1][i];
                            time1[o] = grid[rowgridoffset + 2][i];
                            open1[o] = atof(grid[rowgridoffset + 3][i]);
                            high1[o] = atof(grid[rowgridoffset + 4][i]);
                            low1[o] = atof(grid[rowgridoffset + 5][i]);
                            close1[o] = atof(grid[rowgridoffset + 6][i]);
                            volume1[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date1[o]);
                            std::string timeastr (time1[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
                second=0;
                datetimeserial1[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial5[o]=dateserial (year, month, day);
                            //datetimeserial5[o]=datetimeserial5[o]*24*60*60;
                            //datetimeserial5[o]=datetimeserial5[o]+(hour*60*60);
                            //datetimeserial5[o]=datetimeserial5[o]+(minute*60);
                            o ++;
                        }
                        break;
     
     case 5:


                        
                        o = 0;                        

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date5[o] = grid[rowgridoffset + 1][i];
                            time5[o] = grid[rowgridoffset + 2][i];
                            open5[o] = atof(grid[rowgridoffset + 3][i]);
                            high5[o] = atof(grid[rowgridoffset + 4][i]);
                            low5[o] = atof(grid[rowgridoffset + 5][i]);
                            close5[o] = atof(grid[rowgridoffset + 6][i]);
                            volume5[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date5[o]);
                            std::string timeastr (time5[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial5[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial5[o]=dateserial (year, month, day);
                            //datetimeserial5[o]=datetimeserial5[o]*24*60*60;
                            //datetimeserial5[o]=datetimeserial5[o]+(hour*60*60);
                            //datetimeserial5[o]=datetimeserial5[o]+(minute*60);
                            o ++;
                        }
                        break;
        case 15:


                        
                        o = 0;

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date15[o] = grid[rowgridoffset + 1][i];
                            time15[o] = grid[rowgridoffset + 2][i];
                            open15[o] = atof(grid[rowgridoffset + 3][i]);
                            high15[o] = atof(grid[rowgridoffset + 4][i]);
                            low15[o] = atof(grid[rowgridoffset + 5][i]);
                            close15[o] = atof(grid[rowgridoffset + 6][i]);
                            volume15[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date15[o]);
                            std::string timeastr (time15[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial15[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial15[o]=dateserial (year, month, day);
                            //datetimeserial15[o]=datetimeserial15[o]*24*60*60;
                            //datetimeserial15[o]=datetimeserial15[o]+(hour*60*60);
                            //datetimeserial15[o]=datetimeserial15[o]+(minute*60);
                            o ++;
                        }
                        break;
        case 30:


                        
                        o = 0;

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date30[o] = grid[rowgridoffset + 1][i];
                            time30[o] = grid[rowgridoffset + 2][i];
                            open30[o] = atof(grid[rowgridoffset + 3][i]);
                            high30[o] = atof(grid[rowgridoffset + 4][i]);
                            low30[o] = atof(grid[rowgridoffset + 5][i]);
                            close30[o] = atof(grid[rowgridoffset + 6][i]);
                            volume30[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date30[o]);
                            std::string timeastr (time30[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial30[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial30[o]=dateserial (year, month, day);
                            //datetimeserial30[o]=datetimeserial30[o]*24*60*60;
                            //datetimeserial30[o]=datetimeserial30[o]+(hour*60*60);
                            //datetimeserial30[o]=datetimeserial30[o]+(minute*60);
                            o ++;
                        }
                        break;
        case 60:


                        
                        o = 0;

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date60[o] = grid[rowgridoffset + 1][i];
                            time60[o] = grid[rowgridoffset + 2][i];
                            open60[o] = atof(grid[rowgridoffset + 3][i]);
                            high60[o] = atof(grid[rowgridoffset + 4][i]);
                            low60[o] = atof(grid[rowgridoffset + 5][i]);
                            close60[o] = atof(grid[rowgridoffset + 6][i]);
                            volume60[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date60[o]);
                            std::string timeastr (time60[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial60[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial60[o]=dateserial (year, month, day);
                            //datetimeserial60[o]=datetimeserial60[o]*24*60*60;
                            //datetimeserial60[o]=datetimeserial60[o]+(hour*60*60);
                            //datetimeserial60[o]=datetimeserial60[o]+(minute*60);
                            o ++;
                        }
                        break;
        case 240:


                        
                        o = 0;

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date240[o] = grid[rowgridoffset + 1][i];
                            time240[o] = grid[rowgridoffset + 2][i];
                            open240[o] = atof(grid[rowgridoffset + 3][i]);
                            high240[o] = atof(grid[rowgridoffset + 4][i]);
                            low240[o] = atof(grid[rowgridoffset + 5][i]);
                            close240[o] = atof(grid[rowgridoffset + 6][i]);
                            volume240[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date240[o]);
                            std::string timeastr (time240[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial240[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial240[o]=dateserial (year, month, day);
                            //datetimeserial240[o]=datetimeserial240[o]*24*60*60;
                            //datetimeserial240[o]=datetimeserial240[o]+(hour*60*60);
                            //datetimeserial240[o]=datetimeserial240[o]+(minute*60);
                            o ++;
                        }
                        break;
        case 1440:

                        
                        o = 0;

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date1440[o] = grid[rowgridoffset + 1][i];
                            time1440[o] = grid[rowgridoffset + 2][i];
                            open1440[o] = atof(grid[rowgridoffset + 3][i]);
                            high1440[o] = atof(grid[rowgridoffset + 4][i]);
                            low1440[o] = atof(grid[rowgridoffset + 5][i]);
                            close1440[o] = atof(grid[rowgridoffset + 6][i]);
                            volume1440[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date1440[o]);
                            std::string timeastr (time1440[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial1440[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial1440[o]=dateserial (year, month, day);
                            //datetimeserial1440[o]=datetimeserial1440[o]*24*60*60;
                            //datetimeserial1440[o]=datetimeserial1440[o]+(hour*60*60);
                            //datetimeserial1440[o]=datetimeserial1440[o]+(minute*60);
                            o ++;
                        }
                        break;
        case 10080:

                        
                        o = 0;
                       

                        for (i = chartbars[displayedfile];i>=ii;i--) {                            
                            date10080[o] = grid[rowgridoffset + 1][i];
                            time10080[o] = grid[rowgridoffset + 2][i];
                            open10080[o] = atof(grid[rowgridoffset + 3][i]);
                            high10080[o] = atof(grid[rowgridoffset + 4][i]);
                            low10080[o] = atof(grid[rowgridoffset + 5][i]);
                            close10080[o] = atof(grid[rowgridoffset + 6][i]);
                            volume10080[o] = atoi(grid[rowgridoffset + 7][i]);                       
                            std::string dateastr (date10080[o]);
                            std::string timeastr (time10080[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);                                                                                                                                                                    
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial10080[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial10080[o]=dateserial (year, month, day);
                            //datetimeserial10080[o]=datetimeserial10080[o]*24*60*60;
                            //datetimeserial10080[o]=datetimeserial10080[o]+(hour*60*60);
                            //datetimeserial10080[o]=datetimeserial10080[o]+(minute*60);
                            o ++;
                        }
                        break;
        case 43200:


                        
                        o = 0;

                        for (i = chartbars[displayedfile];i>=ii;i--) {
                            date43200[o] = grid[rowgridoffset + 1][i];
                            time43200[o] = grid[rowgridoffset + 2][i];
                            open43200[o] = atof(grid[rowgridoffset + 3][i]);
                            high43200[o] = atof(grid[rowgridoffset + 4][i]);
                            low43200[o] = atof(grid[rowgridoffset + 5][i]);
                            close43200[o] = atof(grid[rowgridoffset + 6][i]);
                            volume43200[o] = atoi(grid[rowgridoffset + 7][i]);
                            std::string dateastr (date43200[o]);
                            std::string timeastr (time43200[o]);
                            std::string yearstr = dateastr.substr (0,4);
                            std::string monthstr = dateastr.substr (5,2);
                            std::string daystr = dateastr.substr (8,2);
                            std::string hourstr = timeastr.substr (0,2);
                            std::string minutestr = timeastr.substr (3,2);
                            year=atol(yearstr.c_str());                                                                             
                            month=atol(monthstr.c_str());                            
                            day=atol(daystr.c_str());                            
                            hour=atoi(hourstr.c_str());                            
                            minute=atoi(minutestr.c_str());
			    second=0;
			    datetimeserial43200[o]=calculate_seconds_since_1_1_1970(year,month,day,hour,minute,second); //dateserial (year, month, day);
                            //datetimeserial43200[o]=dateserial (year, month, day);
                            //datetimeserial43200[o]=datetimeserial43200[o]*24*60*60;
                            //datetimeserial43200[o]=datetimeserial43200[o]+(hour*60*60);
                            //datetimeserial43200[o]=datetimeserial43200[o]+(minute*60);
                            o ++;
                        }
                        break;
 }

 return 0;
}

char* idate(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  date1[shift];
    break;
    case 5:
    return  date5[shift];
    break;
    case 15:
    return  date15[shift];
    break;
    case 30:
    return  date30[shift];
    break;
    case 60:
    return  date60[shift];
    break;
    case 240:
    return  date240[shift];
    break;
    case 1440:
    return  date1440[shift];
    break;
    case 10080:
    return  date10080[shift];
    break;
    case 43200:
    return  date43200[shift];
    break;
                   }
return 0;                   
}

char* itime(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  time1[shift];
    break;
    case 5:
    return  time5[shift];
    break;
    case 15:
    return  time15[shift];
    break;
    case 30:
    return  time30[shift];
    break;
    case 60:
    return  time60[shift];
    break;
    case 240:
    return  time240[shift];
    break;
    case 1440:
    return  time1440[shift];
    break;
    case 10080:
    return  time10080[shift];
    break;
    case 43200:
    return  time43200[shift];
    break;
                   }
return 0;                   
}    

double iopen(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  open1[shift];
    break;
    case 5:
    return  open5[shift];
    break;
    case 15:
    return  open15[shift];
    break;
    case 30:
    return  open30[shift];
    break;
    case 60:
    return  open60[shift];
    break;
    case 240:
    return  open240[shift];
    break;
    case 1440:
    return  open1440[shift];
    break;
    case 10080:
    return  open10080[shift];
    break;
    case 43200:
    return  open43200[shift];
    break;
                   }
return 0;                   
}

double ihigh(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  high1[shift];
    break;
    case 5:
    return  high5[shift];
    break;
    case 15:
    return  high15[shift];
    break;
    case 30:
    return  high30[shift];
    break;
    case 60:
    return  high60[shift];
    break;
    case 240:
    return  high240[shift];
    break;
    case 1440:
    return  high1440[shift];
    break;
    case 10080:
    return  high10080[shift];
    break;
    case 43200:
    return  high43200[shift];
    break;
                   }
return 0;                   
}

double ilow(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  low1[shift];
    break;
    case 5:
    return  low5[shift];
    break;
    case 15:
    return  low15[shift];
    break;
    case 30:
    return  low30[shift];
    break;
    case 60:
    return  low60[shift];
    break;
    case 240:
    return  low240[shift];
    break;
    case 1440:
    return  low1440[shift];
    break;
    case 10080:
    return  low10080[shift];
    break;
    case 43200:
    return  low43200[shift];
    break;
                   }
return 0;                   
}

double iclose(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  close1[shift];
    break;
    case 5:
    return  close5[shift];
    break;
    case 15:
    return  close15[shift];
    break;
    case 30:
    return  close30[shift];
    break;
    case 60:
    return  close60[shift];
    break;
    case 240:
    return  close240[shift];
    break;
    case 1440:
    return  close1440[shift];
    break;
    case 10080:
    return  close10080[shift];
    break;
    case 43200:
    return  close43200[shift];
    break;
                   }
return 0;                   
}

int ivolume(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  volume1[shift];
    break;
    case 5:
    return  volume5[shift];
    break;
    case 15:
    return  volume15[shift];
    break;
    case 30:
    return  volume30[shift];
    break;
    case 60:
    return  volume60[shift];
    break;
    case 240:
    return  volume240[shift];
    break;
    case 1440:
    return  volume1440[shift];
    break;
    case 10080:
    return  volume10080[shift];
    break;
    case 43200:
    return  volume43200[shift];
    break;
                   }
return 0;                   
}

double idatetimeserial(int timeframe,int shift)
{
switch (timeframe) {
    case 1:
    return  datetimeserial1[shift];
    break;
    case 5:
    return  datetimeserial5[shift];
    break;
    case 15:
    return  datetimeserial15[shift];
    break;
    case 30:
    return  datetimeserial30[shift];
    break;
    case 60:
    return  datetimeserial60[shift];
    break;
    case 240:
    return  datetimeserial240[shift];
    break;
    case 1440:
    return  datetimeserial1440[shift];
    break;
    case 10080:
    return  datetimeserial10080[shift];
    break;
    case 43200:
    return  datetimeserial43200[shift];
    break;
                   }
return 0;                   
}

double iapplied_price(int applied_price,int timeframe,int shift)
{
switch (applied_price) {
    case 0: //PRICE_CLOSE
    return  iclose(timeframe,shift);
    break;
    case 1: //PRICE_OPEN
    return  iopen(timeframe,shift);
    break;
    case 2: //PRICE_HIGH
    return  ihigh(timeframe,shift);
    break;
    case 3: //PRICE_LOW
    return  ilow(timeframe,shift);
    break;
    case 4: //PRICE_MEDIAN
    return  (ihigh(timeframe,shift)+ilow(timeframe,shift))/2;
    break;
    case 5: //PRICE_TYPICAL
    return  (ihigh(timeframe,shift)+ilow(timeframe,shift)+iclose(timeframe,shift))/3;
    break;
    case 6: //PRICE_WEIGHTED
    return  (ihigh(timeframe,shift)+ilow(timeframe,shift)+iclose(timeframe,shift)+iclose(timeframe,shift))/4;
    break;
                   }
return 0;                   
}

double itype(int mode,int timeframe,int shift)
{
switch (mode) {
    case 0: //MODE_OPEN
    return  iopen(timeframe,shift);
    break;
    case 1: //MODE_LOW
    return  ilow(timeframe,shift);
    break;
    case 2: //MODE_HIGH
    return  ihigh(timeframe,shift);
    break;
    case 3: //MODE_CLOSE
    return  iclose(timeframe,shift);
    break;
    //case 4: //MODE_VOLUME
    //return  ivolume(timeframe,shift);
    //break;
    //case 5: //MODE_TIME
    //return  itime(timeframe,shift);
    //break;
                   }
return 0;                   
}

double irvi(int timeframe,int period,int mode,int shift)
{

arrayofdoubles RVI;
arrayofdoubles Trigger;
arrayofdoubles Value1;
arrayofdoubles Value2;

int Length = period;
int buffers = 0;
int drawBegin = 0;

if (bars <= drawBegin) return (0);
    //int countedBars = IndicatorCounted();
    //if (countedBars < 0) return (-1);
    //if (countedBars > 0) countedBars--;
    int s, limit = 333; //bars-1;// - countedBars - 1;
    for (s = limit; s >= 0; s--) {
        Value1[s] = ((iclose(timeframe,s) - iopen(timeframe,s)) + 
            2.0 * (iclose(timeframe,s + 1) - iopen(timeframe,s + 1)) + 
            2.0 * (iclose(timeframe,s + 2) - iopen(timeframe,s + 2)) + 
            (iclose(timeframe,s + 3) - iopen(timeframe,s + 3))) / 6.0;
        Value2[s] = ((ihigh(timeframe,s) - ilow(timeframe,s)) + 
            2.0 * (ihigh(timeframe,s + 1) - ilow(timeframe,s + 1)) + 
            2.0 * (ihigh(timeframe,s + 2) - ilow(timeframe,s + 2)) + 
            (ihigh(timeframe,s + 3) - ilow(timeframe,s + 3))) / 6.0;
        double Num = 0.0;
        double Denom = 0.0;
	int count = 0;
        for (count = 0; count < Length; count++) {
            Num += Value1[s + count];
            Denom += Value2[s + count];
        }        
        if (Denom != 0.0) {
            RVI[s] = Num / Denom;
        } else {
            RVI[s] = 0.0;
        }
	if (mode==MODE_MAIN && s==shift) { return RVI[s]; break; }
        // Trigger[s] = RVI[s + 1]; // Here was the bug
	Trigger[s] = (RVI[s]+2*RVI[s+1]+2*RVI[s+2]+RVI[s+3])/6;
	if (mode==MODE_SIGNAL && s==shift) { return Trigger[s]; break; }	
	
    }
    return (0);   

}

double iadx(int timeframe,int period,int applied_price,int mode,int shift)
{
int ADXPeriod=period;
//---- buffers
arrayofdoubles ADXBuffer;
arrayofdoubles PlusDiBuffer;
arrayofdoubles MinusDiBuffer;
arrayofdoubles PlusSdiBuffer;
arrayofdoubles MinusSdiBuffer;
arrayofdoubles TempBuffer;

double pdm,mdm,tr;
   double price_high,price_low;
   int    starti,i; //,counted_bars=IndicatorCounted();
//----
   i=333-2; //bars-2;
   PlusSdiBuffer[i+1]=0;
   MinusSdiBuffer[i+1]=0;
   //if(counted_bars>=i) i=Bars-counted_bars-1;

   starti=i;
//----
   while(i>=0)
     {
      price_low=ilow(timeframe,i);
      price_high=ihigh(timeframe,i);
      //----
      pdm=price_high-ihigh(timeframe,i+1);
      mdm=ilow(timeframe,i+1)-price_low;
      if(pdm<0) pdm=0;  // +DM
      if(mdm<0) mdm=0;  // -DM
      if(pdm==mdm) { pdm=0; mdm=0; }
      else if(pdm<mdm) pdm=0;
           else if(mdm<pdm) mdm=0;
      //---- âû÷èñëÿåì èñòèííûé èíòåðâàë
      double num1=fabs(price_high-price_low);
      double num2=fabs(price_high-iclose(timeframe,i+1));
      double num3=fabs(price_low-iclose(timeframe,i+1));
      tr=mathmax(num1,num2);
      tr=mathmax(tr,num3);
      //---- counting plus/minus direction
      if(tr==0) { PlusSdiBuffer[i]=0; MinusSdiBuffer[i]=0; }
      else      { PlusSdiBuffer[i]=100.0*pdm/tr; MinusSdiBuffer[i]=100.0*mdm/tr; }
      //----
      i--;
     }
//---- last counted bar will be recounted
   //if(counted_bars>0) counted_bars--;
   int limit=333; //bars; //-counted_bars;
//---- apply EMA to +DI
   for(i=0; i<=limit; i++)
      { 
        //PlusDiBuffer[i]=imaonarray(PlusSdiBuffer,bars,ADXPeriod,0,MODE_EMA,i);
	PlusDiBuffer[i]=imaonarray(PlusSdiBuffer,333,ADXPeriod,0,MODE_EMA,i);
	if (mode==MODE_PLUSDI && i==shift) { return PlusDiBuffer[i]; break; }
      }
//---- apply EMA to -DI
   for(i=0; i<=limit; i++)
      { 
        //MinusDiBuffer[i]=imaonarray(MinusSdiBuffer,bars,ADXPeriod,0,MODE_EMA,i);
	MinusDiBuffer[i]=imaonarray(MinusSdiBuffer,333,ADXPeriod,0,MODE_EMA,i);
	if (mode==MODE_MINUSDI && i==shift) { return MinusDiBuffer[i]; break; }
      }
//---- Directional Movement (DX)
   i=333-2; //bars-2;
   TempBuffer[i+1]=0;
   i=starti;
   while(i>=0)
     {
      double div=fabs(PlusDiBuffer[i]+MinusDiBuffer[i]);
      if(div==0.00) TempBuffer[i]=0;
      else TempBuffer[i]=100*(fabs(PlusDiBuffer[i]-MinusDiBuffer[i])/div);
      i--;
     }
//---- ADX is exponential moving average on DX
   for(i=0; i<limit; i++) 
    {
      //ADXBuffer[i]=imaonarray(TempBuffer,bars,ADXPeriod,0,MODE_EMA,i);
	ADXBuffer[i]=imaonarray(TempBuffer,333,ADXPeriod,0,MODE_EMA,i);
	if (mode==MODE_MAIN && i==shift) { return ADXBuffer[i]; break; }
    }
//----
   return(0);
}

double ima(int timeframe,int period,int ma_shift,int ma_method,int applied_price,int shift)
{
int p=1000;
arrayofdoubles ExtMapBuffer;
double sum;
int i,k;

switch(ma_method)
     {
      case 0 : 
      // SMA      
    int per;
    sum=0;    
    if (p<period) p=period;
    per=period;

    for (i=1;i<=per-1;i++) {
        sum=sum+iapplied_price(applied_price,timeframe,p);
        p--;
                           }
    while (p>=0) {
        sum=sum+iapplied_price(applied_price,timeframe,p);
        if (p==shift+ma_shift) { return sum/per; break; }
        sum=sum-iapplied_price(applied_price,timeframe,p+per-1);
        p--;
                 }
      break;

      case 1 : // Exponential Moving Average  
	double pr;
	pr=2.0/(period+1);
	//---- main calculation loop
   	while(p>=0)
     	{
      	if(p==1000) ExtMapBuffer[p+1]=iapplied_price(applied_price,timeframe,p+1);
      	ExtMapBuffer[p]=iapplied_price(applied_price,timeframe,p)*pr+ExtMapBuffer[p+1]*(1-pr);
	if (p==shift+ma_shift) { return ExtMapBuffer[p]; break; }
 	   p--;
     	}	
	break;

      case 2 : // Smoothed Moving Average 
	sum=0;	   
	//---- main calculation loop
	   while(p>=0)
	     {
	      if(p==1000)
	        {
	         //---- initial accumulation
	         for(i=0,k=p;i<period;i++,k++)
	           {
	            sum+=iapplied_price(applied_price,timeframe,k);
	            //---- zero initial bars
	            ExtMapBuffer[k]=0;
	           }
	        }
	      else sum=ExtMapBuffer[p+1]*(period-1)+iapplied_price(applied_price,timeframe,p);
	      ExtMapBuffer[p]=sum/period;
		if (p==shift+ma_shift) { return ExtMapBuffer[p]; break; }
	 	p--;
	     }
	break;

      case 3 : // Linear Weighted Moving Average
	sum=0.0; double lsum=0.0;
	   double price;
	   int    weight=0;
	//---- initial accumulation
	   if(p<period) p=period;
	   for(i=1;i<=period;i++,p--)
	     {
	      price=iapplied_price(applied_price,timeframe,p);
	      sum+=price*i;
	      lsum+=price;
	      weight+=i;
	     }
	//---- main calculation loop
	   p++;
	   i=p+period;
	   while(p>=0)
	     {
	      ExtMapBuffer[p]=sum/weight;
		if (p==shift+ma_shift) { return ExtMapBuffer[p]; break; }
	      if(p==0) break;
	      p--;
	      i--;
	      price=iapplied_price(applied_price,timeframe,p);
	      sum=sum-lsum+price*period;
	      lsum-=iapplied_price(applied_price,timeframe,i);
	      lsum+=price;
	     }
	break;
     }

    
return 0;
}

double irsi(int timeframe,int period,int applied_price,int shift)
{
int RSIPeriod=period;
//---- buffers
static arrayofdoubles RSIBuffer;
static arrayofdoubles PosBuffer;
static arrayofdoubles NegBuffer;

int    i,counted_bars=cntbarsedit;
   double rel,negative,positive;
int Bars=bars;
//----
   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;
//----
   //i=Bars; //-RSIPeriod-1;
   //if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
	i=999;
   while(i>=0)
     {
      double sumn=0.0,sump=0.0;
      if(i==Bars-RSIPeriod-1)
        {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=lowa[k]-lowa[k+1];
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         rel=lowa[i]-lowa[i+1];
         if(rel>0) sump=rel;
         else      sumn=-rel;
         positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
         negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
        }
      PosBuffer[i]=positive;
      NegBuffer[i]=negative;
      if(negative==0.0) RSIBuffer[i]=0.0;
      else RSIBuffer[i]=100.0-100.0/(1+positive/negative);
      if (i==shift) { return RSIBuffer[i]; break; }
      i--;
     }
//----
   return(0);
}

double istochastic(int timeframe,int kperiod,int dperiod,int slowing,int method,int price_field,int mode,int shift)
{
//int RSIPeriod=period;
//---- buffers
arrayofdoubles MainBuffer;
arrayofdoubles SignalBuffer;
arrayofdoubles HighesBuffer;
arrayofdoubles LowesBuffer;
//---- input parameters
//extern int KPeriod=5;
//extern int DPeriod=3;
//extern int Slowing=3;
int draw_begin1=0;
int draw_begin2=0;
draw_begin1=kperiod+slowing;
draw_begin2=draw_begin1+dperiod;

int    i,k;
   int    counted_bars=cntbarsedit;
   double price;

//int    i,counted_bars=cntbarsedit;
//   double rel,negative,positive;
//int Bars=bars;
//---- minimums counting
   i=333; //bars-kperiod;
   //if(counted_bars>KPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double min=1000000;
      k=i+kperiod-1;
      while(k>=i)
        {
         price=lowa[k];
         if(min>price) { min=price; }
         k--;
        }
      LowesBuffer[i]=min;
      i--;
     }

//---- maximums counting
   i=333; //bars-kperiod;
   //if(counted_bars>KPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double max=-1000000;
      k=i+kperiod-1;
      while(k>=i)
        {
         price=higha[k];
         if(max<price) { max=price; }
         k--;
        }
      HighesBuffer[i]=max;
      i--;
     }

//---- %K line
   i=333; //bars-draw_begin1;
   //if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumlow=0.0;
      double sumhigh=0.0;
      int kres=i+slowing-1;
      for(k=kres;k>=i;k--)
        {
         sumlow+=closea[k]-LowesBuffer[k];
         sumhigh+=HighesBuffer[k]-LowesBuffer[k];
        }
      if(sumhigh==0.0) { MainBuffer[i]=100.0; }
      else { MainBuffer[i]=sumlow/sumhigh*100; }

      if (i==shift) { return MainBuffer[i]; break; }

      i--;
     }

//---- last counted bar will be recounted
   //if(counted_bars>0) counted_bars--;
   //int limit=333;
//---- signal line is simple movimg average
   //for(i=0; i<limit; i++)
     // { //SignalBuffer[i]=imaonarray(MainBuffer,bars,dperiod,0,MODE_SMA,i); 
	
      //}
//----

   return(0);
}

double icci(int timeframe,int period,int applied_price,int shift)
{
int CCIPeriod = period;
int Bars=333;
//---- buffers
static arrayofdoubles CCIBuffer;
static arrayofdoubles RelBuffer;
static arrayofdoubles DevBuffer;
static arrayofdoubles MovBuffer;

int    i, k;
   double price, sum, mul; 
   if(CCIPeriod <= 1)
       return(0);
   if(Bars <= CCIPeriod) 
       return(0);
//---- last counted bar will be recounted
   int limit = Bars;

//---- moving average
   for(i = 0; i < limit; i++)
       MovBuffer[i] = ima(timeframe, CCIPeriod, 0, MODE_SMA, applied_price, i);
//---- standard deviations
   i = Bars - CCIPeriod + 1;
 
   mul = 0.015 / CCIPeriod;
   while(i >= 0)
     {
       sum = 0.0;
       k = i + CCIPeriod - 1;
       while(k >= i)
        {
          price =iapplied_price(applied_price,timeframe,k); //(higha[k] + lowa[k] + closea[k]) / 3;
          sum += fabs(price - MovBuffer[i]);
          k--;
        }
       DevBuffer[i] = sum*mul;
       i--;
     }
   i = Bars - CCIPeriod + 1;

   while(i >= 0)
     {
       price = iapplied_price(applied_price,timeframe,i); //(higha[i] + lowa[i] + closea[i]) / 3;
       RelBuffer[i] = price - MovBuffer[i];
       i--;
     }
//---- cci counting
   i = Bars - CCIPeriod + 1;

   while(i >= 0)
     {
       if(DevBuffer[i] == 0.0) 
           CCIBuffer[i] = 0.0;
       else 
           CCIBuffer[i] = RelBuffer[i] / DevBuffer[i];
	if (i==shift) { return (CCIBuffer[i]); break; }
       i--;
     }
//----
   return(0);
}

double iatr(int timeframe,int period,int shift)
{
int AtrPeriod=period;
arrayofdoubles AtrBuffer;
arrayofdoubles TempBuffer;
int i;//,counted_bars=IndicatorCounted();
//----
   //if(Bars<=AtrPeriod) return(0);
//---- initial zero
   //if(counted_bars<1)
      //for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
int Bars=300;
   i=Bars;//Bars-counted_bars-1;
   while(i>=0)
     {
      double high=ihigh(timeframe,i);
      double low =ilow(timeframe,i);
      if(i==Bars-1) TempBuffer[i]=high-low;
      else
        {
         double prevclose=iclose(timeframe,i+1);
         TempBuffer[i]=mathmax(high,prevclose)-mathmin(low,prevclose);
        }
      i--;
     } 
//----
   //if(counted_bars>0) counted_bars--;
   int limit=Bars;//-counted_bars;
   for(i=0; i<limit; i++) {
	AtrBuffer[i]=imaonarray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);
	if (i==shift) { return (AtrBuffer[i]); break; }
	}
//----
   return(0);
}

int ihighest(int timeframe,int type,int count,int start=0)
{
    
int i;
arrayofdoubles array;
double highest;

    highest = 0;
    for (i = start;i<=start+count-1;i++) {
        array[i]=itype(type,timeframe,i);        
        if (array[i] > highest) highest = array[i];           
    }

    for (i = start;i<=start+count-1;i++) {
            if (array[i]==highest) {
                return i;
                break;
                                  }
        }           
return 0;    
}

int ilowest(int timeframe,int type,int count,int start=0)
{
    
int i;
arrayofdoubles array;
double lowest;

    lowest = itype(type,timeframe,start);;
    for (i = start;i<=start+count-1;i++) {
        array[i]=itype(type,timeframe,i);        
        if (array[i] < lowest) lowest = array[i];           
    }
    for (i = start;i<=start+count-1;i++) {
            if (array[i]==lowest) {
                return i;
                break;
                                  }
        }           
return 0;           
    
}

char* playsoundcpp ( char* sndfreq,char* snddur )
{  
static char sndfreqq[1000];
strncpy(sndfreqq, sndfreq, 1000);
int sndfreqint=atoi(sndfreqq);
static char snddurr[1000];
strncpy(snddurr, snddur, 1000);
int snddurint=atoi(snddurr);
Beep(sndfreqint,snddurint); // hertz, milliseconds     
//cin.get(); // wait 
return 0; 
}

double calculate_seconds_since_1_1_1970(long Y1,long M1,long D1,int H1,int m1,int S1)
{

double dayssince111970;
dayssince111970=dateserial(1970,1,1);

double dayssincespecified;
dayssincespecified=dateserial(Y1,M1,D1);

double daysdif=dayssincespecified-dayssince111970;

double inseconds=daysdif*24*60*60;
inseconds+=H1*60*60+m1*60+S1;

return inseconds;
}

char* timecpp(char* bar)
{
static char barr[1000];
strncpy(barr, bar, 1000);
int barint=atoi(barr);

int year;int month;int day;int hour;int minute;
std::string dateastr (datea[barint]);
std::string timeastr (timea[barint]);
std::string yearstr = dateastr.substr (0,4);
std::string monthstr = dateastr.substr (5,2);
std::string daystr = dateastr.substr (8,2);
std::string hourstr = timeastr.substr (0,2);
std::string minutestr = timeastr.substr (3,2);
year=atoi(yearstr.c_str());                                                                             
month=atoi(monthstr.c_str());                            
day=atoi(daystr.c_str());                            
hour=atoi(hourstr.c_str());                            
minute=atoi(minutestr.c_str());

char catline[1000];
sprintf(catline,"%f",calculate_seconds_since_1_1_1970(year,month,day,hour,minute,0));
return catline;
}

double timeb(int bar)
{

int year;int month;int day;int hour;int minute;
std::string dateastr (datea[bar]);
std::string timeastr (timea[bar]);
std::string yearstr = dateastr.substr (0,4);
std::string monthstr = dateastr.substr (5,2);
std::string daystr = dateastr.substr (8,2);
std::string hourstr = timeastr.substr (0,2);
std::string minutestr = timeastr.substr (3,2);
year=atoi(yearstr.c_str());                                                                             
month=atoi(monthstr.c_str());                            
day=atoi(daystr.c_str());                            
hour=atoi(hourstr.c_str());                            
minute=atoi(minutestr.c_str());

return calculate_seconds_since_1_1_1970(year,month,day,hour,minute,0);
}

double timeminute(double seconds)
{
double inhours=seconds/60/60;
double minutes=(inhours-trunc(inhours))*60;
return minutes;
}

double timehour(double seconds)
{
double indays=seconds/60/60/24;
double hours=(indays-trunc(indays))*24;
return hours;
}

int timedayofweekold(double timeb)
{
//double result;
//result= fmod (static_cast<double>(timeb),7);

/*
if (result==0) return 4;
if (result==1) return 3;
if (result==2) return 2;
if (result==3) return 1;
if (result==4) return 0;
if (result==5) return 6;
if (result==6) return 5;
*/

/*
char* mydate;
mydate=ut_to_date(timeb);
char * writable;

std::string mydatestr (mydate);                 
std::string daystr = mydatestr.substr (0,3);
writable = new char[daystr.size() + 1];
std::copy(daystr.begin(), daystr.end(), writable);
writable[daystr.size()] = '\0'; // don't forget the terminating 0           

if (strcmp(writable,"Mon")==0) { return 1; }
if (strcmp(writable,"Tue")==0) { return 2; }
if (strcmp(writable,"Wed")==0) { return 3; }
if (strcmp(writable,"Thu")==0) { return 4; }
if (strcmp(writable,"Fri")==0) { return 5; }
if (strcmp(writable,"Sat")==0) { return 6; }
if (strcmp(writable,"Sun")==0) { return 0; }
*/

double dayofweek=fmod( ( floor ( timeb / 86400 ) + 4 ) ,7 ) ;

//static char debugmsg[1000];
//sprintf(debugmsg,"%f",dayofweek);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

int dayofweekint;
dayofweekint =  static_cast<int>(dayofweek);

//sprintf(debugmsg,"%i",dayofweekint);MessageBox( NULL, debugmsg,"Debug",MB_OK);

return dayofweekint;

}

int timeday(double timeb)
{

char* mydate;
mydate=unix_time_to_datecpp(timeb);
char * writable;

std::string mydatestr (mydate);                 
std::string daystr = mydatestr.substr (8,2);
writable = new char[daystr.size() + 1];
std::copy(daystr.begin(), daystr.end(), writable);
writable[daystr.size()] = '\0'; // don't forget the terminating 0           

std::string::size_type sz;   // alias of size_t

//int i_dec = stoi (daystr,&sz);
int i_dec = boost::lexical_cast<int>(daystr);

return i_dec;


}

int timemonth(double timeb)
{

char* mydate;
mydate=unix_time_to_datecpp(timeb);
char * writable;

std::string mydatestr (mydate);                 
std::string daystr = mydatestr.substr (5,2);
writable = new char[daystr.size() + 1];
std::copy(daystr.begin(), daystr.end(), writable);
writable[daystr.size()] = '\0'; // don't forget the terminating 0           

int i_dec = boost::lexical_cast<int>(daystr);

return i_dec;

}

double mathmax(double value1,double value2)
{

if (value1>value2) return value1;
if (value2>value1) return value2;
if (value1==value2) return value1;

}

double mathmin(double value1,double value2)
{

if (value1<value2) return value1;
if (value2<value1) return value2;
if (value1==value2) return value1;

}

double itimeb(int period,int bar)
{

int year;int month;int day;int hour;int minute;
std::string dateastr;
std::string timeastr;
switch (period)
{

case 1:
dateastr= (date1[bar]);
timeastr= (time1[bar]);
break;    
    
case 5:
dateastr= (date5[bar]);
timeastr= (time5[bar]);
break;

case 15:
dateastr= (date15[bar]);
timeastr= (time15[bar]);
break;

case 30:
dateastr= (date30[bar]);
timeastr= (time30[bar]);
break;

case 60:
dateastr= (date60[bar]);
timeastr= (time60[bar]);
break;

case 240:
dateastr= (date240[bar]);
timeastr= (time240[bar]);
break;

case 1440:
dateastr= (date1440[bar]);
timeastr= (time1440[bar]);
break;

case 10080:
dateastr= (date10080[bar]);
timeastr= (time10080[bar]);
break;

case 43200:
dateastr= (date43200[bar]);
timeastr= (time43200[bar]);
break;
}
std::string yearstr = dateastr.substr (0,4);
std::string monthstr = dateastr.substr (5,2);
std::string daystr = dateastr.substr (8,2);
std::string hourstr = timeastr.substr (0,2);
std::string minutestr = timeastr.substr (3,2);
year=atoi(yearstr.c_str());                                                                             
month=atoi(monthstr.c_str());                            
day=atoi(daystr.c_str());                            
hour=atoi(hourstr.c_str());                            
minute=atoi(minutestr.c_str());

return calculate_seconds_since_1_1_1970(year,month,day,hour,minute,0);
}

char* unix_time_to_date_old(char* unix_time)
{
    //sprintf(debugmsg,"%s","attempt");MessageBox( NULL, debugmsg,"Debug",MB_OK);
//static char unix_timee[1000];
//strncpy(unix_timee, unix_time, 1000);

	//const char time_as_str[] = "1296575549:573352";
	//time_t t = atol(unix_timee); // convert to time_t, ignores msec
    //boost::gregorian::date date = boost::posix_time::from_time_t(t).date();
	//char asctimechar[1000];
	//sprintf(asctimechar,"%s",asctime(gmtime(&t)));
    //sprintf(asctimechar,"%s",date);
    //char* datechar = static_cast<char*>(date);
    //sprintf(debugmsg,"%s",date);MessageBox( NULL, debugmsg,"Debug",MB_OK);
    //sprintf(debugmsg,"%s",asctimechar);MessageBox( NULL, debugmsg,"Debug",MB_OK);
    //char * writable;
    //writable = new char[date.size() + 1];
    //std::copy(date.begin(), date.end(), writable);
    //writable[date.size()] = '\0'; // don't forget the terminating 0
    //sprintf(debugmsg,"%s",writable);MessageBox( NULL, debugmsg,"Debug",MB_OK);
    //char* c = static_cast<char*>( date );
    //sprintf(debugmsg,"%s",c);MessageBox( NULL, debugmsg,"Debug",MB_OK);    
    //sprintf(debugmsg,"%s",boost::lexical_cast<std::string>(date));MessageBox( NULL, debugmsg,"Debug",MB_OK);
    //cout << date << "\n";
	//return 0;

static char unix_timee[1000];
strncpy(unix_timee, unix_time, 1000);

	//const char time_as_str[] = "1296575549:573352";
	time_t t = atoi(unix_timee); // convert to time_t, ignores msec
	char asctimechar[1000];
	sprintf(asctimechar,"%s",asctime(gmtime(&t)));
	return asctimechar;

}

char* unix_time_to_date(char* DateSerialchar)
  {


static char DateSerialcharr[1000];
    strncpy(DateSerialcharr, DateSerialchar, 1000);
    double dblDateSerialchar=atof(DateSerialcharr);

 double DateSerialdbl=dblDateSerialchar;
static long Yearlng;
static long Monthlng;
static long Daylng;
static long Hourlng;
static long Minutelng;
static double Seconddbl;

//DateSerialdbl=1111111111;
static char debugmsg[1000];
//sprintf(debugmsg,"%f",DateSerialdbl);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

DateFromSerial(DateSerialdbl , Yearlng , Monthlng , Daylng,Hourlng,Minutelng,Seconddbl);

long Secondlng=Seconddbl;

//sprintf(debugmsg,"%u",Yearlng);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

char Yearlngchar[1000];
sprintf(Yearlngchar,"%u",Yearlng);

char Monthlngchar[1000];
sprintf(Monthlngchar,"%u",Monthlng);

char Daylngchar[1000];
sprintf(Daylngchar,"%u",Daylng);

char Hourlngchar[1000];
sprintf(Hourlngchar,"%u",Hourlng);

char Minutelngchar[1000];
sprintf(Minutelngchar,"%u",Minutelng);

char Seconddblchar[1000];
sprintf(Seconddblchar,"%u",Secondlng);


//std::string Yearlngcharstr (Yearlngchar);
//std::string Monthlngcharstr (Monthlngchar);
//std::string Daylngcharstr (Daylngchar);
//std::string Hourlngcharstr (Hourlngchar);
//std::string Minutelngcharstr (Minutelngchar);
//std::string Seconddblcharstr (Seconddblchar);

//std::string Yearlngcharstr=boost::lexical_cast<std::string>(Yearlngchar);
//std::string Monthlngcharstr=boost::lexical_cast<std::string>(Monthlngchar);
//std::string Daylngcharstr=boost::lexical_cast<std::string>(Daylngchar);
//std::string Hourlngcharstr=boost::lexical_cast<std::string>(Hourlngchar);
//std::string Minutelngcharstr=boost::lexical_cast<std::string>(Minutelngchar);
//std::string Seconddblcharstr=boost::lexical_cast<std::string>(Seconddblchar);

char yearn[1000];
char monthn[1000];
char dayn[1000];
char hourn[1000];
char minuten[1000];
char secondn[1000];

std::size_t size = std::strlen( Yearlngchar );
if (size==1) {
strcpy (yearn,"000");
strcat (yearn,Yearlngchar);
//Yearlngcharstr="000"+Yearlngcharstr;
}
if (size==2) {
strcpy (yearn,"00");
strcat (yearn,Yearlngchar);
//Yearlngcharstr="00"+Yearlngcharstr;
}
if (size==3) {
strcpy (yearn,"0");
strcat (yearn,Yearlngchar);
//Yearlngcharstr="0"+Yearlngcharstr;
}
if (size==4) {
strcpy (yearn,Yearlngchar);
//Yearlngcharstr="0"+Yearlngcharstr;
}

size = std::strlen( Monthlngchar );
if (size==1) {
strcpy (monthn,"0");
strcat (monthn,Monthlngchar);
//Monthlngcharstr="0"+Monthlngcharstr;
}
if (size==2) {
strcpy (monthn,Monthlngchar);
//Monthlngcharstr="0"+Monthlngcharstr;
}

size = std::strlen( Daylngchar );
if (size==1) {
strcpy (dayn,"0");
strcat (dayn,Daylngchar);
//Daylngcharstr="0"+Daylngcharstr;
}
if (size==2) {
strcpy (dayn,Daylngchar);
//Daylngcharstr="0"+Daylngcharstr;
}

size = std::strlen( Hourlngchar );
if (size==1) {
strcpy (hourn,"0");
strcat (hourn,Hourlngchar);
//Hourlngcharstr="0"+Hourlngcharstr;
}
if (size==2) {
strcpy (hourn,Hourlngchar);
//Hourlngcharstr="0"+Hourlngcharstr;
}

size = std::strlen( Minutelngchar );
if (size==1) {
strcpy (minuten,"0");
strcat (minuten,Minutelngchar);
//Minutelngcharstr="0"+Minutelngcharstr;
}
if (size==2) {
strcpy (minuten,Minutelngchar);
//Minutelngcharstr="0"+Minutelngcharstr;
}

size = std::strlen( Seconddblchar );
if (size==1) {
strcpy (secondn,"0");
strcat (secondn,Seconddblchar);
//Seconddblcharstr="0"+Seconddblcharstr;
}
if (size==2) {
strcpy (secondn,Seconddblchar);
//Seconddblcharstr="0"+Seconddblcharstr;
}

//sprintf(debugmsg,"%s",monthn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",dayn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",hourn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",minuten);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",secondn);MessageBox( NULL, debugmsg,"Debug",MB_OK);

//std::string fulldatetime;

//fulldatetime=Yearlngcharstr+"-"+Monthlngcharstr+"-"+Daylngcharstr+" "+Hourlngcharstr+":"+Minutelngcharstr;

//char fulldatetimechar = new char[1000];
char fulldatetimechar[1000];
strcpy (fulldatetimechar,yearn);
strcat (fulldatetimechar,"-");
strcat (fulldatetimechar,monthn);
strcat (fulldatetimechar,"-");
strcat (fulldatetimechar,dayn);
strcat (fulldatetimechar," ");
strcat (fulldatetimechar,hourn);
strcat (fulldatetimechar,":");
strcat (fulldatetimechar,minuten);
//strcat (fulldatetimechar,":");
//strcat (fulldatetimechar,secondn);

//std::string fulldatetimecharstr=boost::lexical_cast<std::string>(fulldatetimechar);
//std::string fulldatetimecharstr(1, fulldatetimechar); std::cout << fulldatetimecharstr << std::endl;
//std::cout << std::string(1, fulldatetimechar) << std::endl;

//sprintf(debugmsg,"%s",monthn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",dayn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",hourn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",minuten);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",secondn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",fulldatetimechar);MessageBox( NULL, debugmsg,"Debug",MB_OK); 


//writable = new char[S1str.size() + 1];
//std::copy(S1str.begin(), S1str.end(), writable);
//writable[S1str.size()] = '\0'; // don't forget the terminating 0


//char *fulldatetimechar = new char[fulldatetime.length() + 1]; // or
//std::strcpy(fulldatetimechar, fulldatetime.c_str());
      
//----
   return(fulldatetimechar);
  }

char* date_to_unix_time(char* parameters)
{

static char parameterss[1000];
strncpy(parameterss, parameters, 1000);

// Parameters decomposition
long Y1;
long M1;
long D1;
int H1;
int m1;
int S1;

char * writable;

std::string linestr (parameterss);  
          
std::size_t found = linestr.find(";");        
std::string Y1str = linestr.substr (0,found);
writable = new char[Y1str.size() + 1];
std::copy(Y1str.begin(), Y1str.end(), writable);
writable[Y1str.size()] = '\0'; // don't forget the terminating 0           
Y1=atol(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string M1str = linestr.substr (0,found);
writable = new char[M1str.size() + 1];
std::copy(M1str.begin(), M1str.end(), writable);
writable[M1str.size()] = '\0'; // don't forget the terminating 0
M1=atol(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string D1str = linestr.substr (0,found);
writable = new char[D1str.size() + 1];
std::copy(D1str.begin(), D1str.end(), writable);
writable[D1str.size()] = '\0'; // don't forget the terminating 0
D1=atol(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string H1str = linestr.substr (0,found);
writable = new char[H1str.size() + 1];
std::copy(H1str.begin(), H1str.end(), writable);
writable[H1str.size()] = '\0'; // don't forget the terminating 0
H1=atoi(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string m1str = linestr.substr (0,found);
writable = new char[m1str.size() + 1];
std::copy(m1str.begin(), m1str.end(), writable);
writable[m1str.size()] = '\0'; // don't forget the terminating 0
m1=atoi(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string S1str = linestr.substr (0,found);
writable = new char[S1str.size() + 1];
std::copy(S1str.begin(), S1str.end(), writable);
writable[S1str.size()] = '\0'; // don't forget the terminating 0
S1=atoi(writable);

double dayssince111970;
dayssince111970=dateserial(1970,1,1);

double dayssincespecified;
dayssincespecified=dateserial(Y1,M1,D1);

double daysdif=dayssincespecified-dayssince111970;

double inseconds=daysdif*24*60*60;
inseconds+=H1*60*60+m1*60+S1;
//inseconds-=3600;

char catline[1000];
sprintf(catline,"%f",inseconds);

return catline;
}

char* timebcpp(int* bar)
{

//static char barr[1000];
//strncpy(barr, bar, 1000);
//int barint=atoi(barr);

double timebar=timeb(*bar);

char catline[1000];
sprintf(catline,"%i",timebar);

return catline;

}

char* calculate_seconds_since_1_1_1970_cpp(char* parameters)
{

static char parameterss[1000];
strncpy(parameterss, parameters, 1000);

// Parameters decomposition
long Y1;
long M1;
long D1;
int H1;
int m1;
int S1;
//double x;
//char* serr;

char * writable;

std::string linestr (parameterss);  
          
std::size_t found = linestr.find(";");        
std::string Y1str = linestr.substr (0,found);
writable = new char[Y1str.size() + 1];
std::copy(Y1str.begin(), Y1str.end(), writable);
writable[Y1str.size()] = '\0'; // don't forget the terminating 0           
Y1=atol(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string M1str = linestr.substr (0,found);
writable = new char[M1str.size() + 1];
std::copy(M1str.begin(), M1str.end(), writable);
writable[M1str.size()] = '\0'; // don't forget the terminating 0
M1=atol(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string D1str = linestr.substr (0,found);
writable = new char[D1str.size() + 1];
std::copy(D1str.begin(), D1str.end(), writable);
writable[D1str.size()] = '\0'; // don't forget the terminating 0
D1=atol(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string H1str = linestr.substr (0,found);
writable = new char[H1str.size() + 1];
std::copy(H1str.begin(), H1str.end(), writable);
writable[H1str.size()] = '\0'; // don't forget the terminating 0
H1=atoi(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string m1str = linestr.substr (0,found);
writable = new char[m1str.size() + 1];
std::copy(m1str.begin(), m1str.end(), writable);
writable[m1str.size()] = '\0'; // don't forget the terminating 0
m1=atoi(writable);

linestr.assign(linestr.begin()+found+1,linestr.end());
found = linestr.find(";");
std::string S1str = linestr.substr (0,found);
writable = new char[S1str.size() + 1];
std::copy(S1str.begin(), S1str.end(), writable);
writable[S1str.size()] = '\0'; // don't forget the terminating 0
S1=atoi(writable);

double dayssince111970;
dayssince111970=dateserial(1970,1,1);

double dayssincespecified;
dayssincespecified=dateserial(Y1,M1,D1);

double daysdif=dayssincespecified-dayssince111970;

double inseconds=daysdif*24*60*60;
inseconds+=H1*60*60+m1*60+S1;

char catline[1000];
sprintf(catline,"%f",inseconds);

return catline;
}

char* current_time()
{

    long int t = static_cast<long int>(time(NULL));
    char timechar[1000];
    sprintf(timechar,"%u",t);
    return timechar;
}

void DoEvents()
{
    MSG msg;
    BOOL result;

    while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
    {
        result = ::GetMessage(&msg, NULL, 0, 0);
        if (result == 0) // WM_QUIT
        {                
            ::PostQuitMessage(msg.wParam);
            break;
        }
        else if (result == -1)
        {
             // Handle errors/exit application, etc.
        }
        else 
        {
            ::TranslateMessage(&msg);
            :: DispatchMessage(&msg);
        }
    }
}

double irsionarray(arrayofdoubles& array,int total,int period,int shift)
{
int RSIPeriod=period;
static arrayofdoubles RSIBuffer;
static arrayofdoubles PosBuffer;
static arrayofdoubles NegBuffer;
static arrayofdoubles ExtMapBuffer;

int    i,counted_bars=cntbarsedit;;

   double rel,negative,positive;
int Bars=total;
   i=total;

   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;
   i=999;
   while(i>=0)
     {
      double sumn=0.0,sump=0.0;
      if(i==Bars-RSIPeriod-1)
        {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=array[k]-array[k+1];
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         rel=array[i]-array[i+1];
         if(rel>0) sump=rel;
         else      sumn=-rel;
         positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
         negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
        }
      PosBuffer[i]=positive;
      NegBuffer[i]=negative;
      if(negative==0.0) {
	RSIBuffer[i]=0.0;
	if (i==shift) { return 0; break; }
	}
      else { 
	RSIBuffer[i]=100.0-100.0/(1+positive/negative);
	if (i==shift) { return RSIBuffer[i]; break; }
	}
      i--;
     }
//----
   return(0);

}

double imaonarray(arrayofdoubles& array,int total,int period,int ma_shift,int ma_method,int shift)
{

static arrayofdoubles ExtMapBuffer;

switch(ma_method)
     {
      case 0 : 
      // SMA
      double sum;
    int per;
    sum=0;
    int i;int p;
    p=total;
    if (p<period) p=period;
    per=period;

    for (i=1;i<=per-1;i++) {
        sum=sum+array[p];
        p--;
                           }
    while (p>=0) {
        sum=sum+array[p];
        if (p==shift+ma_shift) { return sum/per; break; }
        sum=sum-array[p+per-1];
        p--;
                 }
      break;
      case 1 : /*ema*/

	double pr;
	int    pos;
	pr=2.0/(period+1);   	
	pos=total-2;
   	//if(ExtCountedBars>2) pos=total-ExtCountedBars-1;
	//---- main calculation loop
   while(pos>=0)
     {
      if(pos==total-2) { 
	ExtMapBuffer[pos+1]=array[pos+1];
	if (pos==shift+ma_shift) { return ExtMapBuffer[pos+1]; break; }
	}
      ExtMapBuffer[pos]=array[pos]*pr+ExtMapBuffer[pos+1]*(1-pr);
	if (pos==shift+ma_shift) { return ExtMapBuffer[pos]; break; }
 	   pos--;
     }
	break;
      case 2 : /*smma not implemented yet*/ break;
      case 3 : /*lwma*/ 
	sum=0.0; double lsum=0.0;
	   double price;
	   int    weight=0;
	//---- initial accumulation
		p=total;
	   if(p<period) p=period;
	   for(i=1;i<=period;i++,p--)
	     {
	      price=array[p]; //iapplied_price(applied_price,timeframe,p);
	      sum+=price*i;
	      lsum+=price;
	      weight+=i;
	     }
	//---- main calculation loop
	   p++;
	   i=p+period;
	   while(p>=0)
	     {
	      ExtMapBuffer[p]=sum/weight;
		if (p==shift+ma_shift) { return ExtMapBuffer[p]; break; }
	      if(p==0) break;
	      p--;
	      i--;
	      price=array[p]; //iapplied_price(applied_price,timeframe,p);
	      sum=sum-lsum+price*period;
	      lsum-=array[i]; //iapplied_price(applied_price,timeframe,i);
	      lsum+=price;
	     }
	
	break;
     }

    
return 0;
}

 /* Code to find the equation of a conic */
 /*               Tom Davis              */
 /*             April 12, 1996           */

#define cross(a, b, ab) ab[0] = a[1]*b[2] - a[2]*b[1]; \
			ab[1] = a[2]*b[0] - a[0]*b[2]; \
			ab[2] = a[0]*b[1] - a[1]*b[0];

#define abs(x) ((x > 0.0) ? x : (-x))

/* toconic takes five points in homogeneous coordinates, and returns the
 * coefficients of a general conic equation in a, b, c, ..., f:
 * 
 * a*x*x + b*x*y + c*y*y + d*x + e*y + f = 0.
 * 
 * The routine returns 1 on success; 0 otherwise.  (It can fail, for
 * example, if there are duplicate points.
 * 
 * Typically, the points will be finite, in which case the third (w)
 * coordinate for all the input vectors will be 1, although the code
 * deals cleanly with points at infinity.
 * 
 * For example, to find the equation of the conic passing through (5, 0), 
 * (-5, 0), (3, 2), (3, -2), and (-3, 2), set:
 * 
 * p0[0] =  5, p0[1] =  0, p0[2] = 1, 
 * p1[0] = -5, p1[1] =  0, p1[2] = 1, 
 * p2[0] =  3, p2[1] =  2, p2[2] = 1, 
 * p3[0] =  3, p3[1] = -2, p3[2] = 1, 
 * p4[0] = -3, p4[1] =  2, p4[2] = 1.
 *
 * But if you want the equation of the hyperbola that is tangent to the
 * line 2x=y at infinity,  simply make one of the points be the point at
 * infinity along that line, for example:
 *
 * p0[0] = 1, p0[1] = 2, p0[2] = 0.
 */

int toconic(FLOAT p0[3], FLOAT p1[3], FLOAT p2[3], FLOAT p3[3], FLOAT p4[3], 
	FLOAT *a, FLOAT *b, FLOAT *c, FLOAT *d, FLOAT *e, FLOAT *f)
{
    FLOAT L0[3], L1[3], L2[3], L3[3];
    FLOAT A, B, C, Q[3];
    FLOAT a1, a2, b1, b2, c1, c2;
    FLOAT x0, x4, y0, y4, w0, w4;
    FLOAT aa, bb, cc, dd, ee, ff;
    FLOAT y4w0, w4y0, w4w0, y4y0, x4w0, w4x0, x4x0, y4x0, x4y0;
    FLOAT a1a2, a1b2, a1c2, b1a2, b1b2, b1c2, c1a2, c1b2, c1c2;
    
    cross(p0, p1, L0)
    cross(p1, p2, L1)
    cross(p2, p3, L2)
    cross(p3, p4, L3)
    cross(L0, L3, Q)
    A = Q[0]; B = Q[1]; C = Q[2];
    a1 = L1[0]; b1 = L1[1]; c1 = L1[2];
    a2 = L2[0]; b2 = L2[1]; c2 = L2[2];
    x0 = p0[0]; y0 = p0[1]; w0 = p0[2];
    x4 = p4[0]; y4 = p4[1]; w4 = p4[2];
    
    y4w0 = y4*w0;
    w4y0 = w4*y0;
    w4w0 = w4*w0;
    y4y0 = y4*y0;
    x4w0 = x4*w0;
    w4x0 = w4*x0;
    x4x0 = x4*x0;
    y4x0 = y4*x0;
    x4y0 = x4*y0;
    a1a2 = a1*a2;
    a1b2 = a1*b2;
    a1c2 = a1*c2;
    b1a2 = b1*a2;
    b1b2 = b1*b2;
    b1c2 = b1*c2;
    c1a2 = c1*a2;
    c1b2 = c1*b2;
    c1c2 = c1*c2;

    aa = -A*a1a2*y4w0
	 +A*a1a2*w4y0 
	 -B*b1a2*y4w0
	 -B*c1a2*w4w0
	 +B*a1b2*w4y0
	 +B*a1c2*w4w0
	 +C*b1a2*y4y0
	 +C*c1a2*w4y0
	 -C*a1b2*y4y0
	 -C*a1c2*y4w0;

    cc =  A*c1b2*w4w0
         +A*a1b2*x4w0
	 -A*b1c2*w4w0
	 -A*b1a2*w4x0
	 +B*b1b2*x4w0
	 -B*b1b2*w4x0
	 +C*b1c2*x4w0
	 +C*b1a2*x4x0
	 -C*c1b2*w4x0
	 -C*a1b2*x4x0;

    ff =  A*c1a2*y4x0
	 +A*c1b2*y4y0
	 -A*a1c2*x4y0
	 -A*b1c2*y4y0
	 -B*c1a2*x4x0
	 -B*c1b2*x4y0
	 +B*a1c2*x4x0
	 +B*b1c2*y4x0
	 -C*c1c2*x4y0
	 +C*c1c2*y4x0;

    bb =  A*c1a2*w4w0
	 +A*a1a2*x4w0
	 -A*a1b2*y4w0
	 -A*a1c2*w4w0
	 -A*a1a2*w4x0
	 +A*b1a2*w4y0
	 +B*b1a2*x4w0
	 -B*b1b2*y4w0
	 -B*c1b2*w4w0
	 -B*a1b2*w4x0
	 +B*b1b2*w4y0
	 +B*b1c2*w4w0
	 -C*b1c2*y4w0
	 -C*b1a2*x4y0
	 -C*b1a2*y4x0
	 -C*c1a2*w4x0
	 +C*c1b2*w4y0
	 +C*a1b2*x4y0
	 +C*a1b2*y4x0
	 +C*a1c2*x4w0;

    dd = -A*c1a2*y4w0
	 +A*a1a2*y4x0
	 +A*a1b2*y4y0
	 +A*a1c2*w4y0
	 -A*a1a2*x4y0
	 -A*b1a2*y4y0
	 +B*b1a2*y4x0
	 +B*c1a2*w4x0
	 +B*c1a2*x4w0
	 +B*c1b2*w4y0
	 -B*a1b2*x4y0
	 -B*a1c2*w4x0
	 -B*a1c2*x4w0
	 -B*b1c2*y4w0
	 +C*b1c2*y4y0
	 +C*c1c2*w4y0
	 -C*c1a2*x4y0
	 -C*c1b2*y4y0
	 -C*c1c2*y4w0
	 +C*a1c2*y4x0;

    ee = -A*c1a2*w4x0
	 -A*c1b2*y4w0
	 -A*c1b2*w4y0
	 -A*a1b2*x4y0
	 +A*a1c2*x4w0
	 +A*b1c2*y4w0
	 +A*b1c2*w4y0
	 +A*b1a2*y4x0
	 -B*b1a2*x4x0
	 -B*b1b2*x4y0
	 +B*c1b2*x4w0
	 +B*a1b2*x4x0
	 +B*b1b2*y4x0
	 -B*b1c2*w4x0
	 -C*b1c2*x4y0
	 +C*c1c2*x4w0
	 +C*c1a2*x4x0
	 +C*c1b2*y4x0
	 -C*c1c2*w4x0
	 -C*a1c2*x4x0;

    if (aa != 0.0) {
	bb /= aa; cc /= aa; dd /= aa; ee /= aa; ff /= aa; aa = 1.0;
    } else if (bb != 0.0) {
	cc /= bb; dd /= bb; ee /= bb; ff /= bb; bb = 1.0;
    } else if (cc != 0.0) {
	dd /= cc; ee /= cc; ff /= cc; cc = 1.0;
    } else if (dd != 0.0) {
	ee /= dd; ff /= dd; dd = 1.0;
    } else if (ee != 0.0) {
	ff /= ee; ee = 1.0;
    } else {
	return 0;
    }
    *a = aa;
    *b = bb;
    *c = cc;
    *d = dd;
    *e = ee;
    *f = ff;
    return 1;
}

int getdayofyear (char* year,char* month,char* day)
{  
static char yearr[1000];
strncpy(yearr, year, 1000);
static char monthh[1000];
strncpy(monthh, month, 1000);
static char dayy[1000];
strncpy(dayy, day, 1000);
int yearint=atoi(yearr);
int monthint=atoi(monthh);
int dayint=atoi(dayy);
//sprintf(debugmsg,"%i",yearint);MessageBox( NULL, debugmsg,"Debug",MB_OK); 
//sprintf(debugmsg,"%i",monthint);MessageBox( NULL, debugmsg,"Debug",MB_OK); 
//sprintf(debugmsg,"%i",dayint);MessageBox( NULL, debugmsg,"Debug",MB_OK);
boost::gregorian::date d ( yearint,monthint,dayint );
int dayNumber = static_cast<short>(d.day_of_year());
return dayNumber;
}

char* rqmod (char* firstnumber,char* secondnumber)
{  

static char debugmsg[1000];

static char firstnumberr[1000];
strncpy(firstnumberr, firstnumber, 1000);

static char secondnumberr[1000];
strncpy(secondnumberr, secondnumber, 1000);

double firstnumberdbl=atof(firstnumberr);
double secondnumberdbl=atof(secondnumberr);

//sprintf(debugmsg,"%f",firstnumberdbl);MessageBox( NULL, debugmsg,"Debug",MB_OK); 
//sprintf(debugmsg,"%f",secondnumberdbl);MessageBox( NULL, debugmsg,"Debug",MB_OK); 
//sprintf(debugmsg,"%i",dayint);MessageBox( NULL, debugmsg,"Debug",MB_OK);

double resnum;
resnum=fmod (firstnumberdbl,secondnumberdbl);
//sprintf(debugmsg,"%f",resnum);MessageBox( NULL, debugmsg,"Debug",MB_OK); 
char catline[1000];
sprintf(catline,"%f",resnum);
return catline;
}

int DateFromSerial(double DateSerial , long & Year , long & Month , long & Day,long & Hour,long & Minute,double & Second) {
    //declare
    //long daysCount , dayOfMonth , dayTotal;//, isLeapYearlng;

static char debugmsg[1000];
//sprintf(debugmsg,"%f",DateSerial);MessageBox( NULL, debugmsg,"Debug",MB_OK);


long hoursrest=0;
    //string DateSerialstr=Format$("%12.0f", DateSerial)

    double secondsrest=fmod(DateSerial,86400);



    //defstr amodstr="86400"

    //defdbl secondsrest=val(varptr$(rqmod(varptr(DateSerialstr),varptr(amodstr))))

    Second=secondsrest;

    double secondsrest2=secondsrest;

    secondsrest=fmod(secondsrest2, 60);


 
    Second=secondsrest;  

    long minutesrest=secondsrest2 / 60;

 

    long minutesrest2=minutesrest;

    minutesrest=minutesrest2 % 3600;



    Minute=minutesrest;      

    if (minutesrest % 60==0) {
    minutesrest2=minutesrest;
    minutesrest=0;
    }         



    Minute=minutesrest % 60;  

  

    hoursrest=minutesrest2 / 60;



    Hour=hoursrest;       
    
    DateSerial=DateSerial/24/60/60;    
//sprintf(debugmsg,"%f",DateSerial);MessageBox( NULL, debugmsg,"Debug",MB_OK);


    long daysCount=0 , dayOfMonth=0 , dayTotal=0;
    DateSerial=DateSerial+719163;
//sprintf(debugmsg,"%f",DateSerial);MessageBox( NULL, debugmsg,"Debug",MB_OK);

//sprintf(debugmsg,"%u",dayTotal);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%u",dayOfMonth);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%u",daysCount);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%f",DateSerial);MessageBox( NULL, debugmsg,"Debug",MB_OK);


    //initialize
    Year = 1;
    Month = 1;
    daysCount = DateSerial - 1;
//sprintf(debugmsg,"%u",daysCount);MessageBox( NULL, debugmsg,"Debug",MB_OK);


    //pre-process
    if (daysCount > 146096) {
        Year = Year + (trunc(round(daysCount) / 146097)) * 400;
        daysCount = (daysCount % 146097);
    }



    if (daysCount > 36523) {
        dayTotal = trunc(round(daysCount) / 36524);
//sprintf(debugmsg,"%u",dayTotal);MessageBox( NULL, debugmsg,"Debug",MB_OK);
        if (dayTotal < 4) {
            Year = Year + (dayTotal * 100);
            daysCount = (daysCount % 36524);
	}
        else {
             Year = Year + 300;
             daysCount = 36524;
	     }        
    }


 
    if (daysCount > 1460) {
        Year = Year + (trunc(round(daysCount) / 1461)) * 4;
        daysCount = (daysCount % 1461);
    }


 
    if (daysCount > 364) {
        dayTotal = trunc(round(daysCount) / 365);
//sprintf(debugmsg,"%u",dayTotal);MessageBox( NULL, debugmsg,"Debug",MB_OK);
        if (dayTotal < 4) {
            Year = Year + dayTotal;
            daysCount = (daysCount % 365);
	}
        else {
            	Year = Year + 3;
            	daysCount = 365;
        	}
    }



    //process
    Day = daysCount + 1;
    dayOfMonth = 1040154527;
    //isLeapYearlng=isleapyear(Year);
    if ((Day > 58) && (isleapyear(Year))) dayOfMonth = (dayOfMonth + 32);

//sprintf(debugmsg,"%u",dayOfMonth);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%u",dayTotal);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%u",Day);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%u",Month);MessageBox( NULL, debugmsg,"Debug",MB_OK);

    do
	{
        dayTotal = (dayOfMonth & 31);
        if (dayTotal >= Day) break;
        Day = Day - dayTotal;
        Month = Month + 1;
        dayOfMonth = (trunc(round(dayOfMonth) / 32));
        if (dayOfMonth == 0) dayOfMonth = 1072692223;
    	}
    	while (1==1);
//sprintf(debugmsg,"%s","17");MessageBox( NULL, debugmsg,"Debug",MB_OK); 


    //exit
    return (0);
}

long dateserial(long y,long m,long d)
{
    //declare
    long dayscount;long dayofmonth;long year;long month;

    //initialize
    dayscount = d;

    //process
    if (m > 1) {
        month = m;
        dayofmonth = 1040154527;
        if (((month > 2) && isleapyear(y))==1) dayofmonth = (dayofmonth + 32);
        do {
            dayscount = dayscount + (dayofmonth & 31);
            if (month == 2) break;
            month--;
            dayofmonth = trunc(round(dayofmonth) / 32);
            if (dayofmonth == 0) dayofmonth = 1072692223;
           }
        while (1==1);
               }

    //exit
    year = (y - 1);
    return (dayscount + (trunc(year) * 365) + (trunc(round(year) / 4)) - (trunc(round(year) / 100)) + (trunc(round(year) / 400)));
}




int timedayofweek(double DateSerialchar)
  {


//static char DateSerialcharr[1000];
//    strncpy(DateSerialcharr, DateSerialchar, 1000);
    double dblDateSerialchar=DateSerialchar;

 double DateSerialdbl=dblDateSerialchar;
static long Yearlng;
static long Monthlng;
static long Daylng;
static long Hourlng;
static long Minutelng;
static double Seconddbl;

//DateSerialdbl=1111111111;
static char debugmsg[1000];
//sprintf(debugmsg,"%f",DateSerialdbl);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

DateFromSerial(DateSerialdbl , Yearlng , Monthlng , Daylng,Hourlng,Minutelng,Seconddbl);

long Secondlng=Seconddbl;

//sprintf(debugmsg,"%u",Yearlng);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

//arrayoflongs offset;
//arrayofchars week;

//sprintf(debugmsg,"%s","0");MessageBox( NULL, debugmsg,"Debug",MB_OK); 

//char writable[1000];
//std::string week0 = "Sunday";
//writable = new char[week0.size() + 1];
//std::copy(week0.begin(), week0.end(), writable);
//writable[week0.size()] = '\0'; // don't forget the terminating 0
//strcpy (week[0],writable);  

//sprintf(debugmsg,"%s",week[0]);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

//char writable[1000];
//std::size_t size = std::strlen( Yearlngchar );
//strcpy (writable,"Sunday");
//strcpy (week[0],writable);
//sprintf(debugmsg,"%s",week[0]);MessageBox( NULL, debugmsg,"Debug",MB_OK); 
         
//week[0]=writable;
//sprintf(debugmsg,"%s","0");MessageBox( NULL, debugmsg,"Debug",MB_OK); 
//strcpy (week[0],"Sunday");
//strcpy (week[1],"Monday");
//strcpy (week[2],"Tuesday");
//strcpy (week[3],"Wednesday");
//strcpy (week[4],"Thursday");
//strcpy (week[5],"Friday");
//strcpy (week[6],"Saturday");

//std::string s = "Sunday";
//char cstr[s.size() + 1];
//strcpy(cstr, s.c_str());    // or pass &s[0]

//std::vector<std::string> week;
//week.push_back("Sunday");
//week.push_back("Monday");

//char writable[1000];
//std::vector<char> week;
//strcpy (writable,"Sunday");
//week.push_back(writable);


//char test[] = "Sunday";
//std::vector<char> week(test, test + sizeof(test)/sizeof(*test));

//std::vector<char> vbuffer;

//std::vector<char> week;
//std::string s = "Sunday";
//std::copy(s.begin(), s.end(), std::back_inserter(week));

std::vector<std::string> myv;
char mydayofweek0[] = "Sunday";  // This will add a terminating \0 character to the array
myv.push_back(mydayofweek0);
char mydayofweek1[] = "Monday";  // This will add a terminating \0 character to the array
myv.push_back(mydayofweek1);
char mydayofweek2[] = "Tuesday";  // This will add a terminating \0 character to the array
myv.push_back(mydayofweek2);
char mydayofweek3[] = "Wednesday";  // This will add a terminating \0 character to the array
myv.push_back(mydayofweek3);
char mydayofweek4[] = "Thursday";  // This will add a terminating \0 character to the array
myv.push_back(mydayofweek4);
char mydayofweek5[] = "Friday";  // This will add a terminating \0 character to the array
myv.push_back(mydayofweek5);
char mydayofweek6[] = "Saturday";  // This will add a terminating \0 character to the array
myv.push_back(mydayofweek6);

std::vector<char*>  vc;
std::transform(myv.begin(), myv.end(), std::back_inserter(vc), convert);   

//sprintf(debugmsg,"%s",vc[6]);MessageBox( NULL, debugmsg,"Debug",MB_OK);

std::vector<long> offset;
offset.push_back(0);
offset.push_back(31);
offset.push_back(59);
offset.push_back(90);
offset.push_back(120);
offset.push_back(151);
offset.push_back(181);
offset.push_back(212);
offset.push_back(243);
offset.push_back(273);
offset.push_back(304);
offset.push_back(334);

//sprintf(debugmsg,"%u",offset[1]);MessageBox( NULL, debugmsg,"Debug",MB_OK);

long afterFeb,dayOfWeek,aux;

afterFeb = 1;
if (Monthlng > 2) afterFeb = 0;
aux = Yearlng - 1700 - afterFeb;
// dayOfWeek for 1700/1/1 = 5, Friday
dayOfWeek  = 5;
// partial sum of days betweem current date and 1700/1/1
dayOfWeek = dayOfWeek + (aux + afterFeb) * 365;
// leap year correction    
dayOfWeek = dayOfWeek + aux / 4 - aux / 100 + (aux + 100) / 400;     
// sum monthly and day offsets
dayOfWeek = dayOfWeek + offset[Monthlng - 1] + (Daylng - 1);               
dayOfWeek = dayOfWeek % 7; // x %= 3 same as x = x % 3

//sprintf(debugmsg,"%u",dayOfWeek);MessageBox( NULL, debugmsg,"Debug",MB_OK);

//int dayofweekint=(int)dayOfWeek;

int dayofweekint;
dayofweekint =  static_cast<int>(dayOfWeek);

return dayofweekint; //week[dayOfWeek] 

}

char* unix_time_to_datecpp(double DateSerialchar)
  {


//static char DateSerialcharr[1000];
//    strncpy(DateSerialcharr, DateSerialchar, 1000);
    double dblDateSerialchar=DateSerialchar;

 double DateSerialdbl=dblDateSerialchar;
static long Yearlng;
static long Monthlng;
static long Daylng;
static long Hourlng;
static long Minutelng;
static double Seconddbl;

//DateSerialdbl=1111111111;
static char debugmsg[1000];
//sprintf(debugmsg,"%f",DateSerialdbl);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

DateFromSerial(DateSerialdbl , Yearlng , Monthlng , Daylng,Hourlng,Minutelng,Seconddbl);

long Secondlng=Seconddbl;

//sprintf(debugmsg,"%u",Yearlng);MessageBox( NULL, debugmsg,"Debug",MB_OK); 

char Yearlngchar[1000];
sprintf(Yearlngchar,"%u",Yearlng);

char Monthlngchar[1000];
sprintf(Monthlngchar,"%u",Monthlng);

char Daylngchar[1000];
sprintf(Daylngchar,"%u",Daylng);

char Hourlngchar[1000];
sprintf(Hourlngchar,"%u",Hourlng);

char Minutelngchar[1000];
sprintf(Minutelngchar,"%u",Minutelng);

char Seconddblchar[1000];
sprintf(Seconddblchar,"%u",Secondlng);


//std::string Yearlngcharstr (Yearlngchar);
//std::string Monthlngcharstr (Monthlngchar);
//std::string Daylngcharstr (Daylngchar);
//std::string Hourlngcharstr (Hourlngchar);
//std::string Minutelngcharstr (Minutelngchar);
//std::string Seconddblcharstr (Seconddblchar);

//std::string Yearlngcharstr=boost::lexical_cast<std::string>(Yearlngchar);
//std::string Monthlngcharstr=boost::lexical_cast<std::string>(Monthlngchar);
//std::string Daylngcharstr=boost::lexical_cast<std::string>(Daylngchar);
//std::string Hourlngcharstr=boost::lexical_cast<std::string>(Hourlngchar);
//std::string Minutelngcharstr=boost::lexical_cast<std::string>(Minutelngchar);
//std::string Seconddblcharstr=boost::lexical_cast<std::string>(Seconddblchar);

char yearn[1000];
char monthn[1000];
char dayn[1000];
char hourn[1000];
char minuten[1000];
char secondn[1000];

std::size_t size = std::strlen( Yearlngchar );
if (size==1) {
strcpy (yearn,"000");
strcat (yearn,Yearlngchar);
//Yearlngcharstr="000"+Yearlngcharstr;
}
if (size==2) {
strcpy (yearn,"00");
strcat (yearn,Yearlngchar);
//Yearlngcharstr="00"+Yearlngcharstr;
}
if (size==3) {
strcpy (yearn,"0");
strcat (yearn,Yearlngchar);
//Yearlngcharstr="0"+Yearlngcharstr;
}
if (size==4) {
strcpy (yearn,Yearlngchar);
//Yearlngcharstr="0"+Yearlngcharstr;
}

size = std::strlen( Monthlngchar );
if (size==1) {
strcpy (monthn,"0");
strcat (monthn,Monthlngchar);
//Monthlngcharstr="0"+Monthlngcharstr;
}
if (size==2) {
strcpy (monthn,Monthlngchar);
//Monthlngcharstr="0"+Monthlngcharstr;
}

size = std::strlen( Daylngchar );
if (size==1) {
strcpy (dayn,"0");
strcat (dayn,Daylngchar);
//Daylngcharstr="0"+Daylngcharstr;
}
if (size==2) {
strcpy (dayn,Daylngchar);
//Daylngcharstr="0"+Daylngcharstr;
}

size = std::strlen( Hourlngchar );
if (size==1) {
strcpy (hourn,"0");
strcat (hourn,Hourlngchar);
//Hourlngcharstr="0"+Hourlngcharstr;
}
if (size==2) {
strcpy (hourn,Hourlngchar);
//Hourlngcharstr="0"+Hourlngcharstr;
}

size = std::strlen( Minutelngchar );
if (size==1) {
strcpy (minuten,"0");
strcat (minuten,Minutelngchar);
//Minutelngcharstr="0"+Minutelngcharstr;
}
if (size==2) {
strcpy (minuten,Minutelngchar);
//Minutelngcharstr="0"+Minutelngcharstr;
}

size = std::strlen( Seconddblchar );
if (size==1) {
strcpy (secondn,"0");
strcat (secondn,Seconddblchar);
//Seconddblcharstr="0"+Seconddblcharstr;
}
if (size==2) {
strcpy (secondn,Seconddblchar);
//Seconddblcharstr="0"+Seconddblcharstr;
}

//sprintf(debugmsg,"%s",monthn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",dayn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",hourn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",minuten);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",secondn);MessageBox( NULL, debugmsg,"Debug",MB_OK);

//std::string fulldatetime;

//fulldatetime=Yearlngcharstr+"-"+Monthlngcharstr+"-"+Daylngcharstr+" "+Hourlngcharstr+":"+Minutelngcharstr;

//char fulldatetimechar = new char[1000];
char fulldatetimechar[1000];
strcpy (fulldatetimechar,yearn);
strcat (fulldatetimechar,"-");
strcat (fulldatetimechar,monthn);
strcat (fulldatetimechar,"-");
strcat (fulldatetimechar,dayn);
strcat (fulldatetimechar," ");
strcat (fulldatetimechar,hourn);
strcat (fulldatetimechar,":");
strcat (fulldatetimechar,minuten);
//strcat (fulldatetimechar,":");
//strcat (fulldatetimechar,secondn);

//std::string fulldatetimecharstr=boost::lexical_cast<std::string>(fulldatetimechar);
//std::string fulldatetimecharstr(1, fulldatetimechar); std::cout << fulldatetimecharstr << std::endl;
//std::cout << std::string(1, fulldatetimechar) << std::endl;

//sprintf(debugmsg,"%s",monthn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",dayn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",hourn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",minuten);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",secondn);MessageBox( NULL, debugmsg,"Debug",MB_OK);
//sprintf(debugmsg,"%s",fulldatetimechar);MessageBox( NULL, debugmsg,"Debug",MB_OK); 


//writable = new char[S1str.size() + 1];
//std::copy(S1str.begin(), S1str.end(), writable);
//writable[S1str.size()] = '\0'; // don't forget the terminating 0


//char *fulldatetimechar = new char[fulldatetime.length() + 1]; // or
//std::strcpy(fulldatetimechar, fulldatetime.c_str());
      
//----
   return(fulldatetimechar);
}